[UPHPU] short syntax without the else?

Brady Mitchell mydarb at gmail.com
Sat Jan 19 23:11:12 MST 2008


On Jan 19, 2008, at 831PM, Dave Smith wrote
> 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";


Keep in mind that this is a bit more difficult read than the standard:

if($condition == false && $var == "foo")
{
	echo false
}
else
{
	echo "foo";
}

especially if you've got multiple people working on the same code.

Brady


More information about the UPHPU mailing list