In Javascript and Python, 0 || 1 returns 1.
But in PHP, 0 || 1 returns true.
How to do if I want 0 || 1 return 1 in PHP?
another example,
$a is array(array('test'))
I want $a['test'] || $a[0] || array() return array('test'), How to do?
The other answers appear to only care about converting boolean to an integer. I think you really want for the second value to be the result if the first is falsy (
0,false, etc.)?For the other languages’ behaviour, the closest we have in PHP is the short-hand “ternary” operator:
0?:1.That could be used in your script like:
$result = get_something() ?: 'a default';See the ternary operator documentation for details.