Page 1 of 1
OK I know it is easy!
Posted: Tue Oct 14, 2003 4:51 pm
by wesnoel
Ok my question is how do I structure this sort of thing?
Code: Select all
<?php
If $var=this
$thisvar="color val"
else
If $var=that
$thisvar="different color val"
else
If $var=this other thing
$thisvar="another color val"
?>
What I want to do is make $thisvar equal a color value depending on what $var is. $var is coming from form data sent via post.
If anyone understand my chicken scratches please help.
Thanks,
Wes/
Posted: Tue Oct 14, 2003 5:02 pm
by qads
Posted: Tue Oct 14, 2003 5:12 pm
by Gen-ik
Code: Select all
<?php
If ($var=="red")
{
$thisvar="ff0000" ;
}
else if ($var=="green")
{
$thisvar="00ff00" ;
}
else if($var=="blue")
{
$thisvar="0000ff" ;
}
else
{
// $var didn't equal red, green, or blue so set $thisvar to black.
$thisvar="000000";
}
?>
Posted: Tue Oct 14, 2003 5:19 pm
by wesnoel
WOW!
It was alot easier than I thought!
thanks qads! You Rock!
I love switch now!
Posted: Tue Oct 14, 2003 5:32 pm
by giraphe
That way works, but I'd prefer
Code: Select all
<?php
switch($var){
case "red": $thisvar="FF0000"; break;
case "green": $thisvar="00FF00"; break;
case "blue": $thisvar="0000FF"; break;
default: $thisvar="000000";
}
?>
much better.
Posted: Tue Oct 14, 2003 5:51 pm
by richie256
yeah right giraphe, you are faster than me!

Posted: Wed Oct 15, 2003 3:21 am
by twigletmac
Just a note, to avoid being unable to read your code as your projects develop try not to ever have more than one instruction on a line. Yes it makes things more compact, e.g.:
Code: Select all
<?php
switch($var){
case "red": $thisvar="FF0000"; break;
case "green": $thisvar="00FF00"; break;
case "blue": $thisvar="0000FF"; break;
default: $thisvar="000000";
}
?>
but it
will make debugging hell at some point. Especially when you are looking for parse errors on a particular line number, if you have three instructions on a line it's three times the debugging. So the following would be a better way (IMHEO) to structure the above switch statement:
Code: Select all
<?php
switch ($var) {
case 'red':
$thisvar = 'FF0000';
break;
case 'green':
$thisvar = '00FF00';
break;
case 'blue':
$thisvar = '0000FF';
break;
default:
$thisvar = '000000';
}
?>
Spaces around assignment operators (=) and around the parts of the control structure, switch ($var) {, instead of switch($var){ will also help.
Just my 2 penneth,
Mac
Posted: Wed Oct 15, 2003 6:12 pm
by wesnoel
Thanks everyone!
You guys are great. All is well in PHP land tonight!
Wes/
Posted: Wed Oct 15, 2003 6:30 pm
by McGruff
As well as if/else's and switch cases, another option is to use variable fns:
Define a fn for each case:
Code: Select all
<?php
function red()
{
return '#f00';
}
function green()
{
return '#0f0';
}
//..etc
?>
In use:
I'd use this if a switch/case was very long - jumps straight to the required fn rather than checking lots of cases.
However the switch case (or if/else) is easier to read since you can see what's going at a glance. Also, you cannot set a default action with variable fns.
Posted: Thu Oct 16, 2003 2:49 am
by volka
and a "call to undefined function" is a bit harder to trap than a switch-default (not to mention malicious values for $var

)
Posted: Thu Oct 16, 2003 4:13 am
by twigletmac
Aah, but that's where function_exists() comes in handy, to expand on McGruff's example:
Code: Select all
if (function_exists($var)) {
$colour = $var();
} else {
echo 'No function has been defined for '.$var;
}
Mac
Posted: Thu Oct 16, 2003 5:22 am
by volka
of course

I only wanted to point out that you have to do some extra checking before blindly relying on $var(), esp. if it somehow derives from user-input
Code: Select all
<?php
$var = 'session_destroy';
echo (function_exists($var)) ? 'true' : 'false';
?>