1

What I want to understand -

$x = true and false;

var_dump($x);

Answer comes out to be as boolean(true);

But under algebra i have been learning as 1 and 0 is 0

Ran the code in php

Alexandru Severin
  • 5,609
  • 10
  • 40
  • 67
Jignesh Rawal
  • 519
  • 5
  • 16

2 Answers2

6

and has a lower operator precedence than = so what you are executing is:

($x = true) and false;

So the complete expression - which you don't use the result of - will return false, but $x will be true.

jeroen
  • 90,003
  • 21
  • 112
  • 129
3

It is because the and operator does not work as you think: 'AND' vs '&&' as operator

Basically, = has higher precedence.

$x = false && true;
var_dump($x);

returns bool(false).

As does

$x = (false and true);
Community
  • 1
  • 1
Bart Friederichs
  • 32,037
  • 14
  • 96
  • 185