Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php why does or operator return 0 as true

Tags:

php

In the following code:

$a = 0 or 1;
$b = 0 || 1; 
echo "$a, $b"; // 0, 1

Why does $a equal zero, I thought or and || were interchangeable in PHP? What exactly is going on with the or statement to make it return 0?

I would have assume both results would have been 1 making it echo 1, 1.

like image 441
AugmentLogic Avatar asked Oct 22 '14 21:10

AugmentLogic


1 Answers

or is lower precedence than = which is lower precedence than ""

So your code is equivalent to:

($a = 0) or 1;
$b = (0 || 1); 

See the precedence table in the PHP manual.

like image 105
Quentin Avatar answered Sep 21 '22 05:09

Quentin