php new bie Posted May 19, 2009 Share Posted May 19, 2009 This code makes me crazy ??? why the numbers won't work properly when matched !! <?php $rand = rand(1,5); $num = array(0,1,2,3,4,5); $match = "" ; echo "<PRE>"; print_r($num); echo "</PRE>"; echo "rand = ". $rand . "<br />"; switch ($match){ case ($rand == $num['1'] ): $match = "PRE"; echo $match; break; case ($rand == $num['2']): $match = "1st"; echo $match; break; case ($rand == $num['3'] ): $match = "2nd"; echo $match; break; case ($rand == $num['4']): $match = "3rd"; echo $match; break; case ($rand == $num['5']): $match = "4th"; echo $match; break; default : $match = "NAH!"; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/ Share on other sites More sharing options...
jlhaslip Posted May 19, 2009 Share Posted May 19, 2009 The switch is checking the value of $match, which you have declared as = "". Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/#findComment-837409 Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 Use: switch($rand) { /* the rest of your code */ } Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/#findComment-837410 Share on other sites More sharing options...
php new bie Posted May 19, 2009 Author Share Posted May 19, 2009 thanks, when I place $rand in switch function all done .. but what about this if I want the $match in switch function .. is that logically error ? ! Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/#findComment-837413 Share on other sites More sharing options...
Maq Posted May 19, 2009 Share Posted May 19, 2009 Maybe something like: $rand = rand(1,5); echo "rand = ". $rand . " "; switch ($match){ case 0: $match = "PRE"; echo $match; break; case 1: $match = "1st"; echo $match; break; case 2: $match = "2nd"; echo $match; break; case 3: $match = "3rd"; echo $match; break; case 4: $match = "4th"; echo $match; break; default : $match = "NAH!"; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/#findComment-837417 Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 It's not a logical error. What are you trying to do? Switch goes like this: <?php switch ($value) { case $value1: // do something break; case $value2: // do something break; . . . } $value1 and $value2 are supposed to be values that $value (the one inside the switch) can be. So if you put: case ($rand == $num['1'] ): Then you're checking if $match is the value of $rand == $num['1'], which is a boolean value and $match is a string. Also, you don't really need a switch for this. You can use array_search and append the proper suffix. Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/#findComment-837418 Share on other sites More sharing options...
php new bie Posted May 19, 2009 Author Share Posted May 19, 2009 thank you all, it is ok now Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/#findComment-837427 Share on other sites More sharing options...
Maq Posted May 19, 2009 Share Posted May 19, 2009 I'm glad it's ok now... Mind sharing the solution so others can refer to it? Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/#findComment-837436 Share on other sites More sharing options...
php new bie Posted May 19, 2009 Author Share Posted May 19, 2009 Maq, I used Nightslyr's suggestion and it works like a knife in butter xD thank you again and thanks to all. by the way this is a Demo not the real script because the real one is Messy and I need this function in the code so I write this simple one to be more easier to all to understand the problem exactly Quote Link to comment https://forums.phpfreaks.com/topic/158775-solved-switch-problem/#findComment-837450 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.