开发者

How to multiply a number by itself X times?

There is some operator that works like this: 5^2 = 5*5 = 25. I tried using 5^2 which didn't work. I tried searching for any names of this ^ operato开发者_StackOverflow中文版r to no avail. How can I do a calculation like this in PHP?

Thanks!


You can use pow function.

So, your example would be:

echo pow(5, 2);  // will echo 25


You can use POW php function. Code:

pow(5,2)


pow($base, $exp)

http://us2.php.net/manual/en/function.pow.php


Have people really forgotten the Law of Logarithms from their junior-high-school algebra this quickly?

Even if you don’t remember the short-cut pow function, you should still remember how to use logarithms!

 echo exp(log(5) + log(10));  # 5*10
 echo exp(log(5)     * 2);    # 5**2
 echo exp(log(7)     * 7);    # 7**7
 echo exp(log(2.5) * 3.75);   # 2.5**3.75
 echo exp(log(49)  * 0.5);    # sqrt 49
 echo exp(log(125) * (1/3))   # cube root of 125
 echo exp(log(81)  * 0.25);   # 4th root of 81

Somewhere out there, there’s a sliderule calling out your name.


There's a pow function.

E.g.:

pow(2, 8)

Is equal to 2^8 ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜