OK I know it is easy!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

OK I know it is easy!

Post 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/
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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";
}
?>
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

WOW!

It was alot easier than I thought!

thanks qads! You Rock!

I love switch now!
giraphe
Forum Newbie
Posts: 1
Joined: Tue Oct 14, 2003 5:32 pm
Location: Sacramento, CA

Post 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.
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Post by richie256 »

yeah right giraphe, you are faster than me! :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post by wesnoel »

Thanks everyone!

You guys are great. All is well in PHP land tonight! 8)

Wes/
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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:

Code: Select all

<?php
$colour = $var();
?>
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.
Last edited by McGruff on Wed Aug 10, 2005 2:53 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;-) )
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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';
?>
Post Reply