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 is the bitwise OR assignment operator in PHP. It performs a bitwise OR operation between the binary representations of the left and right operands, and immediately assigns the resulting integer or string value back to the left operand.

Syntax

$a |= $b;
This is a syntactic shorthand for the expanded assignment:
$a = $a | $b;

Bitwise Mechanics

When the |= operator is invoked, PHP evaluates the operands at the bit level. For each corresponding bit position in the two operands, the operator applies the following truth table:
  • 0 | 0 yields 0
  • 0 | 1 yields 1
  • 1 | 0 yields 1
  • 1 | 1 yields 1
If either or both bits are 1, the resulting bit is 1. The bit is 0 only if both corresponding bits are 0.

Evaluation Example

$x = 10; // Binary representation: 1010
$y = 6;  // Binary representation: 0110

$x |= $y; 

/* 
Bitwise Evaluation:
  1010  (10)
| 0110  (6)

  1110  (14)
*/

echo $x; // Outputs: 14

Type Handling and Coercion

The behavior of the |= operator depends strictly on the data types of the operands provided:
  1. Integers: The standard bitwise operation is performed on the binary integer values.
  2. Floats: If either operand is a floating-point number, PHP truncates the fractional part, casting the float to an integer before performing the bitwise operation. As of PHP 8.1, performing this implicit conversion on a float with a fractional part emits a deprecation notice (Implicit conversion from float to int loses precision).
  3. Strings (Both Operands): If both the left and right operands are strings, the |= operator performs the bitwise OR operation on the underlying ASCII values of the characters at each corresponding byte position. The length of the resulting string will match the length of the longer string operand.
  4. Mixed Types: In PHP 8.0 and later, attempting a bitwise operation between an integer or float and a completely non-numeric string, or a string with trailing non-numeric characters (e.g., "10 foo"), throws a fatal TypeError (Unsupported operand types: int | string). Strings must be strictly numeric to be implicitly cast to integers during mixed-type bitwise operations.
// String operand example
$str1 = "A"; // ASCII 65, Binary: 01000001
$str2 = " "; // ASCII 32, Binary: 00100000

$str1 |= $str2;

/*
  01000001 ("A")
| 00100000 (" ")

  01100001 ("a" - ASCII 97)
*/

echo $str1; // Outputs: "a"
Master PHP with Deep Grasping Methodology!Learn More