1

If I understood Alena here correctly, she says that

$var1 = true && false;
$var2 = true and false;

Will produce in var_dump($var1, $var2);:

false

true

Alena also said that the above code is basically equal to:

$var1 = (true and false);
($var2 = true) and false;

I admit I failed to understand why the first example is euqal to the second. I mean, why writing the two rows, one above the other, will produce different outputs even though they seem equal in essence, thus should seemingly bring same outputs (false, false).

  • 3
    Read through this post to get good understanding of logical operators and their precedence https://stackoverflow.com/questions/2803321/and-vs-as-operator – Pankaj Gadge Dec 16 '17 at 06:55
  • 1
    Because [PHP operators precedence](http://php.net/manual/en/language.operators.precedence.php), that's why. – axiac Dec 16 '17 at 07:59
  • The question's title is misleading. In isolation, `$x && $y` is the same as `$x and $y`. The trouble starts when they are part of a larger expression. – axiac Dec 16 '17 at 08:01
  • @axiac your comment helped me to understand the principle, thank you. –  Dec 16 '17 at 08:05

1 Answers1

1

This is because operator priority. For example, 1+2*3 is 1+(2*3).

Because and (which is actually and or) have less priority than && and ||, then, = is executed before and:

$bool = true and false
manudicri
  • 176
  • 14