These two methods will also do the job:
$zone = $repo->findOneByCode($code) or $zone = new Zone(); ($zone = $repo->findOneByCode($code)) || ($zone = new Zone());
Note that or and && have different priorities, and therefore we need () in the second example. See http://www.php.net/manual/en/language.operators.logical.php . Example:
// The result of the expression (false || true) is assigned to $e // Acts like: ($e = (false || true)) $e = false || true; // The constant false is assigned to $f and then true is ignored // Acts like: (($f = false) or true) $f = false or true; var_dump($e, $f);
And the result:
bool(true) bool(false)
This is because and and or have a lower priority than = , which means that the assignment will be done first. On the other hand, && and || have a higher priority than = , which means that the logical operation will be performed first, and its result will be assigned to the variable. This is why we cannot write:
$result = mysql_query(...) || die(...);
$result will contain the result of a logical operation (true or false). But when we write:
$result = mysql_query(...) or die(...);
the assignment is performed before the logical operation. And if this is not false, the part after or completely ignored.
source share