Assignment in PHP with a bool expression: strange behavior

Why is $ x true in this status?

$x = true and false;

I have a problem with some variables, but I could reduce the problem to primitive booleans.

Update: As you can see in the answers, the effect is related to operator precedence in PHP. There is also a good explanation of the problem in this matter , which I could not find on the net before I did not know that I had a problem with this and I did not know that there is a difference between '& &' / '||' and 'and' / 'or'.

+1
source share
2 answers

, PHP. '=' , ''.

, :

$x = (true and false); //correct: $x is false now!

($x = true) and false;. $x "" . PHP "" , x. , true and false; . . "false" .

, , '& &' '||'! , "=" , , , "" "". ...

, , $x = 5 + 6;, '+' , '=', . '', '' 'xor'. .

, , JavaScript Java. , , ( JavaScript, Java).

+3

:

$x = true and false;

, . , $x , . = , and.

, :

 ($x = true) and false;

, .

:

 $x = true && false;

, $x - , . $x .

:

$x = (true and false);

? && , and.

0

Source: https://habr.com/ru/post/1667523/


All Articles