Entry
How do conditionals work?
How can I include an if statement inside a command?
Jan 27th, 2000 22:42
Nathan Wallace, Egon Schmid, Rasmus Lerdorf
Here is a "Conditionals for Dummies" summary:
It works like this:
( (condition) ? "true" : "false" )
If the condition is true, then the above line evaluates to "true" and
if the condition is false, it evaluates to "false".
Another way to look at it. Take this very normal example:
if($a==1) print "a is one";
else print "a is not one";
Even your grandmother could probably tell you what this did. You can
rewrite this with a conditional to confuse your grandmother, like this:
print ( ($a==1) ? "a is one" : "a is not one" );
Conditionals are not just for confusing grandmothers, though. As per
Cameron's example, sometimes it is very handy to be able to check a
simple
condition in the middle of a larger expression.