Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

** is new power operator instead of power() in php

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;
like image 680
Mahmoud.Eskandari Avatar asked Feb 15 '14 20:02

Mahmoud.Eskandari


People also ask

Which operators signifies power in PHP?

Many notations use "^" as a power operator, but in PHP (and other C-based languages) that is actually the XOR operator. You need to use this 'pow' function, there is no power operator.

How do you write to the power of 2 in PHP?

echo(pow(-2,-3.2));

Which operator is used for power operations?

Exponent Operator (^)

How does power operator work?

Power (exponent) operatorIt takes in two real numbers as input arguments and returns a single number. The operator that can be used to perform the exponent arithmetic in Python is ** .


1 Answers

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

like image 85
Mahmoud.Eskandari Avatar answered Oct 05 '22 14:10

Mahmoud.Eskandari