Quantcast
Channel: upshots » ActionScript 3
Viewing all articles
Browse latest Browse all 10

PHP vs JS conditionals and ternary

$
0
0

Just a quick note about how PHP conditional operators differs from ECMA (JS and AS)…

The double-pipe ‘OR’ operator, in JS, evaluated to the value of the expression that is not false. In PHP, it evaluates to true if any any expression is true, or false if not (the output will actually be “1″ if true, or “0″ if false – which is how PHP treats any boolean evaluation).

var a = 0;
var b = false;
var c = "string";
alert(a || b || c);  // alerts "string" - the first (and only) expression that evaluates to true...
$a = 0;
$b = false;
$c = "string";
echo $a || $b || $c;  // prints "1" - which just means that the statement does have a true evaluation

In PHP 5.3 and later, you can use a modified ternary expression to simulate the JS logic – just leave out the middle component…

$a = false;
$b = "string";
echo $a ?: $b; // prints "string"

Viewing all articles
Browse latest Browse all 10

Trending Articles