I am trying to pull out of the deep corners of my brain how to do: if ($var = 1) { $secondvar = hi; } elseif ($var = 2) { $secondvar = bye; } PHP: in a single line, I thought i remember some shorthand way of doing it. (possibly using ? or : in the code)
is it possible to add a third part? so simplifying: if ($var == 1) { $secondvar = hi; } elseif ($var == 2) { $secondvar = bye; } elseif ($var == 3) { $secondvar = good luck; } Code (markup):
I don't think you can do three, it's straight boolean I'm pretty sure. Any real reason you want to do it like that though? A lot of coders have never seen that before and most people just do: if ($var) echo 'blah'; else echo 'blah2'; PHP: It's just a bit easier to read in the long run I think.
Dude, you need to balance between being lazy and keeping the code readable But yes, you could do that if you want. $secondvar = ($var == 1) ? "hi" : (($var == 2) ? "bye" : "good luck"); Strictly speaking it needs another check for ($var == 3) because your last statement was an 'elseif' not plain 'else'.
Or even arrays. $foo = array( 1 => 'hi', 2 => 'bye', 3 => 'good luck' ); $secondvar = isset($foo[$var]) ? $foo[$var] : 'default'; PHP: