hasini silva hasini silva - 1 month ago 7
PHP Question

php logical operator 'or' between different data types

<?php

$sample = (False or 436);
echo $sample;
?>


I tried this code to identify and have and idea about how operator precedence effects to code. But I'm totally confusing with the answers I got.

<?php

$sample = (False or 436);
echo $sample;
?>


gives answer as
1


<?php

$sample = (True or 436);
echo $sample;
?>


gives
1
too

<?php

$sample = False or 436;
echo $sample;
?>


gives nothing.

As I could see whatever I put next to the
or
the answer was
1


P.S. I tried them on
http://sandbox.onlinephpfunctions.com/
this online editor.

I need to know the is the actual concept behind it.

Answer

You may want to have a read at PHP Type Comparison Tables as well as the Booleans documentation. Pay special attention to Fred Koschara's notes in the top comment, which explains that the OR operator has lower precedence than the assignment operator. You may instead be looking to use || instead of OR, as the || operator does the same thing, but with higher precedence.