Difference Between && and || Operators in PHP



'|' Bitwise OR operator

'|' operator is a bitwise OR operator and is used to set the bit to 1 if any of the corresponding bit is 1.

'||' Logical Or operator

'||' is a logical Or operator and works on complete operands as whole.

Example

Following example, shows usage of '|' vs '||' operators.

 Live Demo

<!DOCTYPE html>
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
   <?php
      $x = 1; // 0001
      $y = 2; // 0010

      print('$x | $y = ');
      echo $x | $y;
      print("<br/>");
      print('$x || $y = ');
      echo $x || $y;
   ?>
</body>
</html>

Output

$x | $y = 3
$x || $y = 1
Updated on: 2020-01-13T06:35:31+05:30

335 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements