by Mithaldu · 10y ago · 0 comments
|| is a short-cutting or comparison operator which returns the first true thing it encounters.
| is a bitwise or combinator, which takes two sets of flags, and combines them into one set of flags that has all flags activated which were also active in the input sets.
Semantically, | is absolutely the correct choice here.
A better question would be why there's a $2 there, though i hope that's just a typo and means $e.
I wish. The fact that it actually doesn't is one of the things that really bugs me about PHP:
<?php
var_dump(0 || 42);
The above example will output: bool(true) $ php -r 'var_dump(0 || 42);'
bool(true)
$ php -r 'var_dump(0 ?: 42);'
int(42)edit: Just to clarify for anyone else unfamiliar with it:
$ php -r 'var_dump(42 ?: 0);'
int(42) $ php -a
Interactive shell
php > echo 1 || die('die');
1
This echo's 1, it doesn't die.I understand the use of pipes for bitwise, but in this case if you read the code in the example, we're basically saying "if the exception is of type A or of type B", personally I'd be inclined to use || if I didn't already know the syntax.
Choosing a pipe because it's easier for an obscure language feature, is not least astonishment. Commas are used for lists. The pipe is a bad choice among a nice feature.