1.2.8. ** For Exponent

The operator ** calculates exponents, also known as power.

Use it instead of the slower function pow(). This operator was introduced in PHP 5.6. Be aware the the ‘-’ operator has lower priority than the ** operator : this leads to the following confusing result. This is due to the parser that processes separately - and the following number. Since ** has priority, the power operation happens first.

Being an operator, ** is faster than pow(). This is a microoptimisation.

<?php
    $cube = pow(2, 3); // 8

    $cubeInPHP56 = 2 ** 3; // 8
?>

See also Arithmetic Operators.

1.2.8.1. Suggestions

  • Use the ** operator

  • For powers of 2, use the bitshift operators

  • For literal powers of 2, consider using the 0xFFFFFFFFF syntax.

1.2.8.2. Specs

Short name

Php/NewExponent

Rulesets

All, Changed Behavior, Suggestions, php-cs-fixable

Exakat since

0.8.4

PHP Version

With PHP 5.6 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

exponential

Examples

Traq, TeamPass

Available in

Entreprise Edition, Exakat Cloud