|
and ||
- what is the difference between these two operators in PHP?

- 12,251
- 8
- 58
- 92
-
possible duplicate of [What is the diffference between the | and || or operators?](http://stackoverflow.com/questions/35301/what-is-the-diffference-between-the-and-or-operators) – outis Mar 10 '12 at 15:43
-
[PHP Language Reference](http://us2.php.net/langref) : [Operators](http://us2.php.net/langref) – bdl Feb 10 '10 at 01:37
5 Answers

- 776,304
- 153
- 1,341
- 1,358
-
4Bitwise or means that the value of (a|b) is the set of bits that are set in either a or b. Logical or means that the value of (a||b) is true if either a or b is true or both or true, false if neither is true. – i_am_jorf Feb 10 '10 at 01:38
-
1As a smalll side question, in PHP can you use the bitwise or on booleans like you can in Java and C#? The practical difference in those cases is that the || will perform a shortcut where if the left disjunct returns true, the right is not checked, whereas with the single | it will perform the check in both cases. – Jacob Bellamy Feb 10 '10 at 01:43
-
1@Jacob Bellamy : a simple test (`# php -r 'var_dump(true | true);'`) suggests that you can. `bool(true)` is probably being automagically cast to `int(1)`. I'd be a bit hesitant about using that syntax myself, as I wouldn't necessarily expect the other guys I work with to pick up on the subtle difference. Similarly, I'm hesitant to use the `or` operator, which unlike `||`, is lower precedence than assignment. – Frank Farmer Feb 10 '10 at 02:15
-
Could you please help me.I'm doubtful about the use of ' | ' in the following context ( Laravel ). ['email' => 'required|email|unique:email,users'] – Michel Oct 27 '17 at 07:06
-
That's a string. Read the Laravel docs to figure out what it does. – Ignacio Vazquez-Abrams Oct 27 '17 at 07:07
Meaning
|
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).
Examples
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

- 64,916
- 15
- 117
- 140
Just like the & and && operator, the double Operator is a "short-circuit" operator.
For example:
if(condition1 || condition2 || condition3) If condition1 is true, condition 2 and 3 will NOT be checked.
if(condition1 | condition2 | condition3) This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good
performance boost by using them.
There is one big caveat, NullReferences or similar problems. For example:
if(class != null && class.someVar < 20) If class is null, the if-statement will stop after "class != null" is false. If you only use &, it will try to check class.someVar and you get a
nice NullReferenceException. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad,
but it's something to keep in mind.
No one ever uses the single & or | operators though, unless you have a design where each condition is a function that HAS the be
executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The & operator does "run these 3 functions,
and if one of them returns false, execute the else block", while the | does "only run the else block if none return false" - can be useful,
but as said, often it's a design smell.
| operates on the bits of a variable: 2 | 4 = 6
|| operates on The Boolean value of a variable: 2 || 4 = TRUE

- 13,277
- 8
- 41
- 62
| -> binary operator || -> Boolean operator or -> also a Boolean operator with lower precedence
$x = false | true; //will set $x to an integer $x = false || true; //will set $x to true $x = false or true; //will set $x to false exactly the same that: ($x = false) || true;