> ## 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.

# PHP Bitwise AND

The `&` operator in PHP functions in two distinct capacities depending on its syntactic context: as a **Bitwise AND** operator for binary-level evaluation, and as a **Reference** operator for memory aliasing.

## 1. Bitwise AND Operator

When placed between two operands, `&` performs a bitwise AND operation. The behavior and return type depend on the types of the operands:

* **Integer Evaluation:** If the operands are integers (or mixed integer/string), it evaluates their binary representations and returns a new integer. Each bit in the result is set to `1` only if the corresponding bits in *both* operands are `1`. If either bit is `0`, the resulting bit is `0`.
* **String Evaluation:** If *both* operands are strings, the `&` operator performs a bitwise AND operation on the ASCII values of the characters at corresponding byte positions and returns a new string.

```php theme={"dark"}
$intResult = $a & $b;
$strResult = $stringA & $stringB;
```

**Integer Mechanics:**

```php theme={"dark"}
$a = 5;  // Binary: 0101
$b = 3;  // Binary: 0011

$result = $a & $b; 

// Binary evaluation:
//   0101
// & 0011
// ---
//   0001  (Decimal: 1)
```

**String Mechanics:**

```php theme={"dark"}
$str1 = "A"; // ASCII 65: 01000001
$str2 = "B"; // ASCII 66: 01000010

$result = $str1 & $str2; 

// Binary evaluation:
//   01000001
// & 01000010
// -------
//   01000000  (ASCII 64: "@")
```

## 2. Reference Operator

When prepended to a variable, function parameter, closure capture, or function declaration, `&` acts as the reference operator. In modern PHP (PHP 7 and later), it instructs the engine to create separate `zval` structures of type `IS_REFERENCE` that point to a shared `zend_reference` struct, bypassing PHP's default pass-by-value and copy-on-write behaviors.

**Assignment by Reference:**
Binds two variables to the same `zend_reference`. Modifying either variable alters the shared value.

```php theme={"dark"}
$a = 10;
$b = &$a; 
$b = 20; 
// $a evaluates to 20
```

**Pass by Reference:**
Applied in function signatures to bind the caller's variable to the function's local parameter scope. Mutations to the parameter directly affect the original variable.

```php theme={"dark"}
function mutateValue(&$parameter) {
    $parameter++;
}

$value = 5;
mutateValue($value);
// $value evaluates to 6
```

**Return by Reference:**
Applied to a function declaration to return a reference to a variable rather than a copy of its value. The `&` must be present in both the function signature and the receiving assignment.

```php theme={"dark"}
function &getReference() {
    static $internalState = 0;
    return $internalState;
}

$stateAlias = &getReference();
$stateAlias = 42;
// The static $internalState is now 42
```

**Closure Capture by Reference:**
Applied within the `use` construct of an anonymous function (closure) to capture a variable from the parent scope by reference, allowing the closure to mutate the original variable.

```php theme={"dark"}
$counter = 0;
$increment = function() use (&$counter) {
    $counter++;
};

$increment();
// $counter evaluates to 1
```

**Iteration by Reference:**
Applied to the value variable in a `foreach` construct to mutate array elements directly in memory during iteration.

```php theme={"dark"}
$array = [1, 2, 3];
foreach ($array as &$item) {
    $item *= 2;
}
// $array evaluates to [2, 4, 6]
unset($item); // Destroys the reference binding to the last element
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
