Jump to content

SWITCH()


mc_keny

Recommended Posts

Hey guys am trying to learn the switch function i have a slight idea how it works say i have this url

test.php?act=main or test.php?act=login on the test page i have switch($act) { case main: echo "main page"; break; case login: echo "login page"; break; default: echo "welcome"; }

is that right

Link to comment
Share on other sites

More like:

switch($_GET['act'])  {  case main: 	echo "main page";	break;  case login:	echo "login page";	break;  default:	echo "welcome";}

$act will only work if register_globals is on, and runing it on is not a good idea.

Link to comment
Share on other sites

well to my knowledge its on cause its working. .... i see ppl use case 1: case 2: am wondering how the script know witch to select if you have the url test.php?act=maincan you gives me a example of using case 1: case 2 etc

Link to comment
Share on other sites

Now that you've mentioned it, there's also another error, which would be visible if you turn error displaying. You need to place quotes around the string values i.e.

switch($_GET['act'])  {  case 'main': 	echo "main page";	break;  case 'login':	echo "login page";	break;  default:	echo "welcome";}

What you have in each "case" is anything that eventually returns a value. The returned value is compared agains the variable in the switch() statement, and if it is the same, the code below each case is executed until a "break" is found (the code between the case and the break is often reffered to as "block"). So, using numbers instead of strings is the same deal as using anything else. If you use "case 1" and "case 2", but in the URL enter "act=main", then you'll simply activate the "case 'main'" block, and if you remove that, you'll execute the "default" blcok since there won't be any matching block to run.

Link to comment
Share on other sites

thanks a lot bro ok say i have

 $second = date(s); if($second <10){echo "welcome";}else if($second <20){echo "welcome back";}els if($second <30){echo "hi user";}

how would you use case 1 case 2 etc

$sec = date(s); switch(sec){case 1:echo "welcome";break;case 2:echo "welcome back";break;case 3:echo "hi user";break;}

Link to comment
Share on other sites

The switch-case option is done when you have a variable that can be equal to some value.it'd be the same if you make

if ($var==value1){   //code here}elseif ($var==value2){   //code here}

As if you put

switch ($var){   case value1:	//code here	break;   case value2:	//code here	break;}

That's only when the variable is equal to some value, it won't work when you have a <, <=,>,>=,!= , etc...

Link to comment
Share on other sites

Since in your example you are wanting to check if the value is less than 10 (00-09), 20 (10-19), 30 (20-29), they will have the same first digit, you might try checking the value of the first number.

<?php  $sec=date('s');  if ($sec<10) {	$sec='0'.$sec;  }  switch(substr($sec, 0, 1)) {	case 0:	  echo 'welcome';	  break;	case 1:	  echo 'welcome user';	  break;	case 2:	  echo 'hi user';	  break;  }?>

Link to comment
Share on other sites

Ok guys il just continue using tha if statement the reason why i was trying out the switch statement was cause a friend of my use that function bellow and i was suggesting its way more easier than tha if statement but i cant seem to learn it

registerform($ef) { $ue = $errl = $pe = $ce = ""; switch($ef) { case 1: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> Please type your UserID"; $ue = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 2: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> Please type your password"; $pe = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 3: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> Please type your password again"; $ce = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 4: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> UserID is invalid"; $ue = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 5: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> Password is invalid"; $pe = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 6: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> Passwords doesn't match"; $ce = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 7: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> UserID must be 4 characters or more"; $ue = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 8: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> Password must be 4 characters or more"; $pe = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 9: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> UserID already in use, choose a different one"; $ue = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 10: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> Unknown mysql error try registering later"; break; case 11: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> UserID must start with a letter from a-z"; $ue = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; case 12: $errl = "<img src=\"images/point.gif\" alt=\"!\"/> UserID is reserved for admins of the site"; $ue = "<img src=\"images/point.gif\" alt=\"!\"/>"; break; }

Link to comment
Share on other sites

Switch is used for testing a single variable or expression for several possible values. That function above is an error reporting function, you pass an error code between 1 and 12 to it and it displays the error message for that error code. That's all it does. Read the manual page for switch:http://www.php.net/manual/en/control-structures.switch.php

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...