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 arithmetic exponentiation operator in PHP, used to raise a base operand to the power of an exponent operand. Introduced in PHP 5.6, it serves as the native syntactic equivalent to the pow() function.

Syntax

$result = $base ** $exponent;

Associativity

Unlike most mathematical operators in PHP which are left-associative, the ** operator is right-associative. When multiple exponentiation operators are chained in a single expression, the evaluation engine processes them from right to left.
$result = 2 ** 3 ** 2; 

// Evaluated as: 2 ** (3 ** 2) -> 2 ** 9 -> 512
// NOT evaluated as: (2 ** 3) ** 2 -> 8 ** 2 -> 64

Precedence

The ** operator has a higher operator precedence than standard multiplicative (*, /, %) and additive (+, -) operators. Crucially, it also has a higher precedence than the unary negation operator (-). If you intend to raise a negative number to a power, the base must be explicitly wrapped in parentheses.
$a = -2 ** 2;   // Evaluated as -(2 ** 2) -> -4
$b = (-2) ** 2; // Evaluated as (-2) ** 2 -> 4

Type Resolution and Coercion

The return type of the ** operator is dynamically resolved by the Zend Engine based on the operands and the mathematical result:
  • int: Returned if both operands are integers, the exponent is greater than or equal to zero, and the resulting value does not exceed PHP_INT_MAX (integer overflow).
  • float: Returned if either operand is a float, if the exponent is negative (which mathematically yields a fraction), or if the calculated result exceeds the maximum integer bounds.
var_dump(2 ** 3);   // int(8)
var_dump(2 ** -1);  // float(0.5)
var_dump(2.0 ** 3); // float(8)

Compound Assignment

PHP provides a corresponding compound assignment operator (**=) to mutate a variable in place by raising its current value to the power of the right-hand operand.
$base = 4;
$base **= 2; // Equivalent to: $base = $base ** 2;
// $base is now int(16)
Master PHP with Deep Grasping Methodology!Learn More