I'm not sure what to call this, so I'll give an example.
In PHP
1==2 || 2
returns 1
or true
In Ruby
1==2 || 2
returns 2
(The second statement if the first evaluates to false).
Is there any short way to implement similar thing in PHP?
I'm not sure what to call this, so I'll give an example.
In PHP
1==2 || 2
returns 1
or true
In Ruby
1==2 || 2
returns 2
(The second statement if the first evaluates to false).
Is there any short way to implement similar thing in PHP?
In PHP, the result of boolean expressions is always a boolean. So 1==2 || 2
gives true
.
The best thing I can think of is
($var = 1 == 2) || ($var = 2)
Then $var
will be 2
.
Depending on the answer to Matchu's question, you may want:
(($var = 1) == 1) || ($var = 2)
or
($var = 1 == 1) || ($var = 2)
How about 1 == 2 or 2
?
However, the result might not be the same if you print directly, so you need to put the result inside a variable. Take this example:
$result = "a" or 2;
var_dump($result); // prints string(1) "a"
var_dump("a" or 2); // prints bool(true)
Take a look here: http://www.php.net/manual/en/language.operators.logical.php