3

I have a question regarding ** operator which has appeared in PHP 5.6. As far as I understand it, it is used for an exponentiation. However, if we want to increase a negative number:

var_dump(-2 ** 2);
var_dump(pow(-2, 2));

then in the first example the result is -4 and 4 in the second. Is it all right? Or this operator acts differently than pow() function?

  • 2
    http://php.net/manual/en/language.operators.precedence.php `**` has higher precedence than `-`, so it's `-(2**2)`, not `(-2)**2` – Marc B Sep 03 '15 at 20:52

1 Answers1

5

It must be precedence as var_dump((-2) ** 2); yields 4.

So -2 ** 2 raises 2 to the power of 2 and then makes the result 4 negative.

See Operator Precedence

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87