Earlier today I tried to do this:
Example 1:
<?php
echo $myVar || "rawr";
?>
I thought this might print out $myVar
if it was set, and print "rawr"
if not.
It printed a 1
, I assume this is the result of the OR
test.
What I then tried was this:
Example 2:
<?php
if ($myVar)
{
echo $myVar;
}
else
{
echo "rawr";
}
?>
Which is what I was trying to accomplish.
I think I understand why the first prints the results of the OR
test rather than one of the variables, and also why I tried it - been spending some time on the bash shell recently :)
Can anyone tell me if there is a way to perform the text in example #2 but in similar syntax to example #1?
empty($var) ? "rawr" : $var
. There are gazillions of questions here on SO dealing with this usage ofempty
. – Jon Sep 21 '11 at 14:13$myVar
then you should have already set it to some value at the point you first introduce it in the symbol table. If it's an index into an array (like$_GET
), then you can hide this inside a function. – Jon Sep 21 '11 at 14:17