Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The | operator in PHP is the bitwise inclusive OR operator. It evaluates the binary representations of two operands and performs a logical OR operation on each corresponding pair of bits. If a bit is 1 in the left operand, the right operand, or both, the corresponding bit in the result is set to 1. The result bit is 0 only if both corresponding operand bits are 0.
$result = $operand1 | $operand2;

Truth Table

| Bit A | Bit B | Result (A | B) | | :---: | :---: | :---: | | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 |

Integer Evaluation

When operating on integers, the engine aligns the binary sequences of both numbers and evaluates them bit-by-bit.
$a = 5;  // Binary representation: 0000 0101
$b = 3;  // Binary representation: 0000 0011

$result = $a | $b; 
// Evaluation: 0000 0111
// Decimal output: 7

String Evaluation

If both operands are strings, PHP performs the bitwise OR operation byte-by-byte, evaluating the underlying ASCII/byte values of the characters.
$str1 = "A"; // ASCII 65, Binary: 0100 0001
$str2 = " "; // ASCII 32, Binary: 0010 0000

$result = $str1 | $str2; 
// Evaluation: 0110 0001 
// ASCII output: 97 (String: "a")
String Length Discrepancies: If the two string operands are of unequal length, the bitwise operation is performed up to the length of the longer string. The shorter string is treated as if it were padded with null bytes (\x00) to match the length of the longer string. Because a bitwise OR operation between any byte and a null byte (0000 0000) results in the original byte, the remaining trailing bytes of the longer string are appended to the result unmodified.

Type Coercion Rules

The | operator enforces specific type juggling behaviors depending on the operands provided:
  • Float to Integer: If an operand is a floating-point number, PHP truncates the fractional part and casts it to an integer before performing the bitwise operation.
  • Mixed Types (String and Int): The byte-by-byte string evaluation is strictly reserved for when both operands are strings. If one operand is a string and the other is an integer (or float), the string must be a well-formed numeric string, which PHP will cast to an integer prior to evaluation. As of PHP 8.0, if the string is non-numeric, applying the | operator throws a TypeError rather than silently casting the string to 0.
Master PHP with Deep Grasping Methodology!Learn More