[UPHPU] short syntax without the else?
Dave Smith
dave at thesmithfam.org
Sat Jan 19 21:31:34 MST 2008
Wade Preston Shearer wrote:
> …without the that? Like this…
>
> $var = condition ? this;
Nope. It's called the ternary operator, because there are 3 parts to it.
The ':' and the operand after it are required.
You can, however, use the short-circuited && operator like this:
$condition && $var = "this";
Here's an example:
$condition = false;
$var = "bar";
// Here's the magic
$condition && $var = "foo";
echo $var;
If $condition is true, this will output "foo", otherwise, it'll output
"false".
This is called short-circuited because the interpreter is smart enough
to not evaluate the second half of an && if the first half is false.
Be sure to use parentheses when your condition has multiple conditions,
like this:
($condition && $other_condition) && $var = "foo";
Enjoy!
--Dave
More information about the UPHPU
mailing list