|
and ||
- what is the difference between these two operators in PHP?
|
is binary operator, it will binary OR the bits of both the lefthand and righthand values.
||
is a boolean operator, it will short circuit when it encounters 'true' (any non-zero value, this includes non-empty arrays).
print_r(1 | 2) // 3
print_r(1 || 2) // 1
When used with functions:
function numberOf($val) {
echo "$val, ";
return $val;
}
echo numberOf(1) | numberOf(2); // Will print 1, 2, 3
echo numberOf(1) || numberOf(2); // Will print 1, 1