Jump to content

switch statement advance example please.


redarrow

Recommended Posts

I am using the swich statement on my study can you give a way to use the statement in a more advanced way please.

an example of the most poular use for it thank you.

Can you use it on a dropdown box or forms or whatever thank you.

[code]
<?
$num=40;

switch ($num) {
case 1;
echo" num is 1";
break;
case 2;
echo" num is 2";
break;
case 3;
echo"num is 3";
break;
case 4;
echo"num is 4";
break;
case 5;
echo"num is 5";
break;
case 6;
echo"num is 6";
break;
case 7;
echo"num is 7";
break;

default:

echo"num is not 1 to 10";

break;

}

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/4925-switch-statement-advance-example-please/
Share on other sites

here's another simple example, but it's a more useful way to use it:
[code]
function getTimeDiff($time, $format = 'minutes') {
  switch($format) {
    case "minutes":
      $min = 60;
      $diff = floor(($time - time()) / $min);
      break;

    case "hours":
      $hour = 60 * 60;
      $diff = floor(($time - time()) / $hour);
      break;

    case "days":
      $day = 60 * 60 * 24;
      $diff = floor(($time - time()) / $day);
      break;
  }

  return abs($diff) . " $format difference";
}
[/code]

this is a simple time difference function that lets you specify whether to return the difference in minutes, hours or days.
Thank you for the example verry useful.


Is there a way the case statement can retreve information from a dropdown menu if so can you kindly post a reply thank you all.


example

if you had a drop down menu with urls on, can the user choose the correct menu title then that information comes from the case statement a small example, please if possable tried googleing but no joy.
well, you can use a switch statement almost anywhere you could use an if statement, so if i understand your question, you'd want something like this:
[code]
<?php
if (isset($_POST['submit'])) {
  switch($_POST['url']) {
    case "www.google.com":
      $title = "Google";
      break;

    case "www.cnn.com":
      $title = "CNN";
      break;

    default:
      $title = "nothing chosen!";
  }
  echo "You chose: $title!";
}
?>

<form name='myForm' action='' method='post'>
<select name='url'>
<option value=''></option>
<option value='www.google.com'>www.google.com</option>
<option value='www.cnn.com'>www.cnn.com</option>
</select><br />
<input type='submit' value='Check It' name='submit' />
</form>
[/code]

remember that many, if not most cases that you see switch statements used, you could do it just as easily with another method, so be careful not to beat yourself up over using a switch statement for something that could better be handled by another method

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.