PHP program
$x = (False or 123)
echo $x
python code
x= (False or 123)
print(x)
In php answer is 1
and in python answer is 123
.
Why is that?
$x = (False or 123)
echo $x
x= (False or 123)
print(x)
In php answer is 1
and in python answer is 123
.
Why is that?
Python and
and or
do a "McCarthy evaluation" that returns the last value, see https://stackoverflow.com/a/22598675/196206 or Wikipedia.
PHP does also short circuit evaluation but only a boolean value is always returned: http://php.net/manual/en/language.operators.logical.php
PHP: $x = (False or 123). False is false(!), 123 is true, false or true == true, so $x get true (or 1 when you print it.
Python: does or operation first, then assigns result to x. int(False) is 0, int(123) is 123, so it gets 123. In Python, there is a trick were the or return value is the value that made it true, and not the boolean result. See this page: http://www.diveintopython.net/power_of_introspection/and_or.html