Do PHP's logical operators work as JavaScript's? Do PHP's logical operators work as JavaScript's? php php

Do PHP's logical operators work as JavaScript's?


PHP returns true orfalse. But you can emulate JavaScript's r = a || b || c with:

$r = $a ?: $b ?: $c;

Regarding 'ands', something like:

$r = ($a && $a->foo) ? $a->foo->bar : null;


PHP logical operators do not return the value on any of their sides : they will always get you a boolean.

For instance, doing :

$result = $a && $b;

Will always make $result contain a boolean : true or false -- and never $a nor $b.