16

How to use new version of Power operator instead of pow() in new version of php (5.6)? Like:

echo pow(2,3);

Why output of this line is 512 not 64?

2 ** 3 ** 2;
Mahmoud.Eskandari
  • 1,460
  • 3
  • 20
  • 32

1 Answers1

28

There is a sample ** operator in php 5.6 +

$i = 6;

$i **=2; //output 36

$out = $i ** 3 //output 46656

echo 2 ** 3 ** 2; // 512 (not 64) because this line evaluated right to left  => 2 ** (3 ** 2)
echo -3 ** 2; // -9 (not 9)
echo 1 - 3 ** 2; // -8
echo ~3 ** 2; // -10 (not 16)

** is better than pow(,) .
When you try to write a math algorithm. ** is a Powerful Operator.
there's no functional difference between it and pow.
power operator refrence

Mahmoud.Eskandari
  • 1,460
  • 3
  • 20
  • 32
  • 10
    Exactly how is it "better"? Other than being a few less characters, there's no functional difference between it and pow. It's like saying "tool A is better than tool B, because tool A is **PINK**" – Marc B Feb 15 '14 at 20:29
  • @Marc It may be better depending on the purpose it is used for. For example, if you were to take user input for an equation solver, and wanted to use exec, you could simply replace `^` with `**` instead of needing to make sure to place that `)` of `pow()` in the right place. – Anonymous Feb 15 '14 at 20:37
  • @MarcB - Better for performance, no overhead of a function call but explicitly handled in exactly the same way as any other arithmetic operator – Mark Baker Feb 15 '14 at 21:10
  • @Anonymous - simply replacing `^` with `**` and then what? Please not `eval()`.... otherwise, if you're using a proper formula parser then there's no difference between `^` and `**` – Mark Baker Feb 15 '14 at 21:11
  • @Mark Obviously not just using eval. It's not like I'm setting the basis for such a program. I'm just providing an example. – Anonymous Feb 15 '14 at 21:17
  • 17
    Being the author of `**` I can tell you that it's different from `pow()` in the sense that it transparently supports GMP (if compiled); and it can be used wherever a constant expression is expected, such as a default value or class constant. – Ja͢ck Jul 21 '14 at 14:30
  • @Ja͢ck, but isn't pow() function implementation replaced by pow_function() call(and supports gmp too) in the same PR? – vp_arth Jul 23 '18 at 19:37
  • @vp_arth tbf, i have not tried that ... you might be right, actually :) – Ja͢ck Jul 23 '18 at 23:09
  • 6
    Note that the output of the third line in the sample code should read `$out = $i ** 3 //output 46656`, not 216, since `$i **= 2` _assigns_ 36 to `$i`, thus `$out = 36 ** 3`. :) – Marten Koetsier Nov 11 '18 at 10:38
  • Why is echo 2 ** 3 ** 2; // 512 but not 64 ? – Salines Dec 13 '19 at 15:46
  • @jirigracik logic :) – Salines Jun 17 '20 at 14:13
  • Just clarifying (for anyone other future readers)... that `2 ** 3 ** 2` is evaluated as `2 ** (3 ** 2)` which is 512 whereas `(2 ** 3) ** 2` is 64, presumably because exponents are right-associative (they get evaluated right to left) :) – Danny Pearson Feb 05 '22 at 17:07