> ## 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 Type Cast

An operator in PHP is a specific symbol or keyword that instructs the Zend Engine to perform a mathematical, relational, bitwise, or logical operation on one or more operands (values or variables) to produce a single resulting value. Operators are the fundamental constructs used to evaluate expressions and mutate state during script execution.

Operators are structurally classified by their arity (the number of operands they accept):

* **Unary:** Operates on a single operand (e.g., `!$a`).
* **Binary:** Operates on two operands (e.g., `$a + $b`).
* **Ternary:** Operates on three operands (e.g., `$a ? $b : $c`).

## Arithmetic Operators

Perform standard mathematical operations. PHP automatically handles type conversion between integers and floats during evaluation.

```php theme={"dark"}
+$a;      // Identity (converts to int or float)
-$a;      // Negation
$a + $b;  // Addition
$a - $b;  // Subtraction
$a * $b;  // Multiplication
$a / $b;  // Division (returns float if not evenly divisible)
$a % $b;  // Modulo (operands are cast to int before processing)
$a ** $b; // Exponentiation
```

## Assignment Operators

Bind a value to a variable. The base assignment operator (`=`) evaluates to the assigned value, allowing chained assignments.

```php theme={"dark"}
$a = $b;   // Basic assignment
$a += $b;  // Addition assignment (syntax applies to -, *, /, %, **, ., &, |, ^, <<, >>)
$a ??= $b; // Null coalescing assignment (assigns $b to $a if $a is null)
```

## Comparison Operators

Evaluate the relationship between two operands, returning a boolean.

```php theme={"dark"}
$a == $b;  // Equal (type juggling/coercion applied)
$a === $b; // Identical (strict type and value equality)
$a != $b;  // Not equal (alternative syntax: <>)
$a !== $b; // Not identical
$a < $b;   // Less than
$a > $b;   // Greater than
$a <= $b;  // Less than or equal to
$a >= $b;  // Greater than or equal to
$a <=> $b; // Spaceship (returns int: -1 if <, 0 if ==, 1 if >)
```

## Logical Operators

Evaluate boolean logic. PHP implements short-circuit evaluation for these operators.

```php theme={"dark"}
!$a;       // Logical NOT
$a && $b;  // Logical AND (high precedence)
$a || $b;  // Logical OR (high precedence)
$a and $b; // Logical AND (low precedence, evaluates after assignment operators)
$a or $b;  // Logical OR (low precedence)
$a xor $b; // Exclusive OR (true if either $a or $b is true, but not both)
```

## Bitwise Operators

Perform operations on the binary representations of integer or string operands. When applied to strings, the bitwise operators evaluate the ASCII values of the characters.

```php theme={"dark"}
~$a;       // Bitwise NOT (inverts bits)
$a & $b;   // Bitwise AND
$a | $b;   // Bitwise OR (inclusive)
$a ^ $b;   // Bitwise XOR (exclusive)
$a << $b;  // Shift left (shifts $a's bits $b steps to the left)
$a >> $b;  // Shift right (shifts $a's bits $b steps to the right)
```

## String Operators

Handle string concatenation.

```php theme={"dark"}
$a . $b;   // Concatenation
$a .= $b;  // Concatenation assignment
```

## Array Operators

Evaluate and manipulate arrays. The union operator (`+`) behaves uniquely by appending the right-hand array to the left-hand array; if keys exist in both arrays, the elements from the left-hand array are preserved, and the duplicate keys from the right-hand array are ignored.

```php theme={"dark"}
$a + $b;   // Union (combines arrays, ignoring duplicate keys from $b)
$a == $b;  // Equality (true if $a and $b have the same key/value pairs)
$a === $b; // Identity (true if $a and $b have the same key/value pairs in the same order and of the same types)
$a != $b;  // Inequality (alternative syntax: <>)
$a !== $b; // Non-identity
```

## Increment/Decrement Operators

Mutate a variable's value by one. The position of the operator determines whether the value is returned before or after the mutation.

```php theme={"dark"}
++$a;      // Pre-increment (increments, then returns $a)
$a++;      // Post-increment (returns $a, then increments)
--$a;      // Pre-decrement (decrements, then returns $a)
$a--;      // Post-decrement (returns $a, then decrements)
```

## Error Control and Execution Operators

Specialized operators for environment and error handling.

```php theme={"dark"}
@$a;       // Error control (suppresses diagnostic errors/warnings for the specific expression)
`$a`;      // Execution (executes contents as a shell command, identical to shell_exec())
```

## Conditional and Null-Handling Operators

Provide shorthand syntax for control structures and null-state checks.

```php theme={"dark"}
$a ? $b : $c; // Ternary (evaluates $b if $a is true, otherwise $c)
$a ?: $c;     // Elvis (evaluates $a if $a is true, otherwise $c)
$a ?? $b;     // Null coalescing (evaluates $a if it exists and is not null, otherwise $b)
$a?->b;       // Nullsafe (evaluates method/property 'b' if $a is not null, otherwise returns null)
```

## Type Operator

Evaluates object inheritance and interface implementation.

```php theme={"dark"}
$a instanceof ClassName; // Returns true if $a is an object of ClassName, its descendants, or implements the specified interface
```

## Precedence and Associativity

When multiple operators are present in a single expression, PHP relies on **precedence** and **associativity** rules:

* **Precedence:** Determines the grouping of operators with different priorities (e.g., `**` binds tighter than `*`, which binds tighter than `+`).
* **Associativity:** Determines the evaluation direction for operators of equal precedence. Most arithmetic operators are left-associative (`$a - $b - $c` evaluates as `($a - $b) - $c`), but the exponentiation operator (`**`) is right-associative (`2 ** 3 ** 2` evaluates as `2 ** (3 ** 2)`). Assignment operators are also right-associative (`$a = $b = $c` evaluates as `$a = ($b = $c)`).
* **Non-associativity:** Certain operators cannot be chained. Notably, as of PHP 8.0, the ternary operator (`? :`) is non-associative. Chaining ternary operators without explicit parentheses results in a fatal error.

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