‘|’ Operator
It is a bitwise OR operator. This operator is used to set the bits of operands if either a or b or both are set. It means the value of the bit will be set to 1.
| A | B | A | B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Syntax:
$a | $b
Program:
<?php $a = 3; $b = 10; echo $a | $b; ?> |
11
Explanation:
In above example, given two values, a = 3 and b = 10. Then convert both the numbers into binary number i.e a = 0011 and b = 1010. Apply OR (|) operation and compute the value of $a | $b.
‘||’ Operator
This is the logical OR operator. This operator is used to do the OR operation. If either of the bits will be 1, then the value of OR will be 1.
Syntax:
$a || $b
Program:
<?php $a = 3; $b = 10; if($a = 3 || $b = 0) echo '1'; else echo '0'; ?> |
1
Explanation: Here the values for the variables are set. Check if either of the condition is true or not, since the value of a in the if statement is true as it is set to be 3, the OR operator will execute to be true and ‘1’ will be displayed.
Note The key difference in the working of the two operators and the nature are same. The bitwise OR operator sets the bit value whereas the logical OR operator sets true or 1 if either one of the conditions/bit value is 1 else it sets false or 0.
Recommended Posts:
- Ternary operator vs Null coalescing operator in PHP
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Difference between concat() and + operator in Java
- Difference between != and !== operator in JavaScript
- 'AND' vs '&&' as operator in PHP
- PHP 7 | Spaceship Operator
- PHP | $ vs $$ operator
- Double not (!!) operator in PHP
- PHP | Ternary Operator
- Scope Resolution operator in PHP
- Difference and Similarities between PHP and C
- What is JavaScript >>> Operator and How to use it ?
- Difference between JavaScript and Php
- Difference between try-catch and if-else statements in PHP
- Difference between isset() and array_key_exists() Function in PHP
- What is the difference between public, private, and protected in PHP?
- Difference between require-dev and require in PHP?
- What is the difference between HTTP_HOST and SERVER_NAME in PHP?
- What is the difference between is_a() function and instanceof in PHP?
- What is the difference between echo, print, and print_r in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

