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
  • 6,021
  • 11
  • 48
  • 71
Jignesh Rawal
  • 521
  • 6
  • 17

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
  • 91,079
  • 21
  • 114
  • 132
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
  • 33,050
  • 15
  • 95
  • 195