4

I understand there are fairly robust math packages for PHP, but what I'm trying to do isn't that complex. I'm looking to calculate the Cube Root of a number. Solving for x in (x^3 = 100) would normally require echo 100^(1/3). Running this code in PHP returns neither an error or a correct number.

JustinP
  • 1,351
  • 1
  • 14
  • 29
  • 2
    why are you writing and immediately answering questions that are trivially found in the PHP manual? The site timers suggest that you posted the answer within a couple of seconds of posting the question. – Alnitak Jan 08 '13 at 16:10
  • I didn't think it was that trivial. I wrote code using the ^ operator and didn't realize for a while my calculations were off. Also, when I searched, I didn't find what I'd expect to be a quick solution for this. I think now there will be. – JustinP Jan 08 '13 at 16:12
  • I actually didn't even do that.. I asked a question with "Answer My own question" selected.. just trying to add to the community. – JustinP Jan 08 '13 at 16:13
  • I don't know _any_ language that uses the `^` operator for exponentiation. – Alnitak Jan 08 '13 at 16:13
  • It wasn't that obvious to me. which is why I made the Stackoverflow entry.. hoping it will help someone like me in the future. – JustinP Jan 08 '13 at 16:15
  • I voted to close, but now I take it back. Sorry. – Lightness Races in Orbit Jan 08 '13 at 16:16
  • 1
    If you wanted to help users, you should have written the question more clearly. A more useful formulation would be "how do I calculate x to the power of y in PHP?". Almost no-one will happen to come here based on a question title asking for cube-roots. As someone else said, self answering is fine, but the question _must_ be usefully phrased. – Alnitak Jan 08 '13 at 16:18
  • Alnitak: MATLAB does. But calling that a "language" is pushing it, I think :) – Hannes Ovrén Jan 08 '13 at 16:18
  • oh, and BTW, a Google search for "php power" gives the right page in the PHP online manual as the top answer. – Alnitak Jan 08 '13 at 16:20

3 Answers3

12

The ^ character is XOR in PHP, a bitwise operator.

To perform power-operations (exponents), you should look into the PHP-defined pow() method. To use this with your sample code, it would be:

echo pow(100, 1/3);

Starting in PHP 5.6, they introduced an exponentiation operator too, **:

echo 100 ** (1/3);
newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • thank you for explaining that `^` is a bitwise operator. Really useful answer. much better than just posting a solution. – JustinP Jan 08 '13 at 16:36
  • take care when using this approach in combination with floor: That doesnt work: `floor( pow( 10648, 1/3))` returns 21 instead of 22. – stot Oct 10 '14 at 10:08
1

PHP Syntax for 100^(1/3) would be:

echo pow(100,1/3);

return would be:

4.6415888336128

learn power about the power and exp.

JustinP
  • 1,351
  • 1
  • 14
  • 29
  • 2
    @danL: This is [actually](http://meta.stackexchange.com/questions/17463/can-i-answer-my-own-questions-even-those-where-i-knew-the-answer-before-asking) [_encouraged_](http://meta.stackexchange.com/questions/12513/should-i-not-answer-my-own-questions) [on Stack Overflow](http://meta.stackexchange.com/questions/17845/etiquette-for-answering-your-own-question). Absolutely nothing wrong with it. – Lightness Races in Orbit Jan 08 '13 at 16:32
  • 1
    I figured out the answer fairly quickly and din't see the question on Stackoverflow. Why is there an option to answer your own question WHILE asking a question.. It kind of stinks I was trying to do something helpful to the community and I got a -1. – JustinP Jan 08 '13 at 16:32
  • @danL: _(continued)_ It's because, contrary to how it might look nowadays with the constant daily localised questions, this isn't some "forum" or "message board" to get your temporary personal issues sorted out via crowdsourcing -- it's a programming Q&A. A database of questions, and their answers. – Lightness Races in Orbit Jan 08 '13 at 16:35
  • @JustinPfister You probably got down-voted because you could have found the function pow() with a quick google search of "php exponents". But at the same time, a lot of people on this site are just out to down-vote anything and everything, so don't worry about it. I'm optimistic on the other hand, so here's an up-vote to make you feel at home. :) – SISYN Jan 08 '13 at 16:37
  • @LightnessRacesinOrbit I understand that, I was just confused as to how he didn't know about the pow() function when he asked the question but now he's giving an answer to it. I understand that you can answer your own question, because it's an option when you post one, but I didn't see why he didn't just answer it in his first post rather than writing a separate answer to his own question. Hope that makes sense. – SISYN Jan 08 '13 at 16:38
  • @danL: thanks for the Positive Reinforcement! I'm not a Computer science guy.. just a fairly dedicated web programmer. I just figured ^ was power. this has been an educational post for me. – JustinP Jan 08 '13 at 16:38
  • @danL: What do you mean "his first post"? He did only write one answer. It's perfectly valid to come up with a question that _others_ may have, and provide the solution at the same time. That's what the OP has done, and that's how our site works. Welcome to SO – Lightness Races in Orbit Jan 08 '13 at 16:41
  • @LightnessRacesinOrbit Sorry for this extended discussion, I know that's not really what comments are for, but what I meant is, when you answer your own question, shouldn't it go in the same post AS the question itself? In this case, the OP posted a question, waited a while (other answers were posted during this time) THEN answered his own question. Just seemed a bit odd to me, that's all. Thanks for the welcome, though, I enjoy it around here! – SISYN Jan 08 '13 at 16:51
  • @danL: No, absolutely not. Stack Overflow doesn't have chronological "posts", but a question followed by an [unordered] set of answers. Answers should _never_ go into the question. :) Also, the OP did not wait a while - the question and answer timestamps are exactly the same, because he used the SO option to submit an answer _with_ the question. – Lightness Races in Orbit Jan 08 '13 at 16:57
  • @danL: _(continued)_ [Sometimes](http://stackoverflow.com/questions/7334952/will-using-goto-leak-variables) [I'll post](http://stackoverflow.com/questions/7067793/what-is-this-crazy-c11-syntax) [a question](http://stackoverflow.com/questions/6438086/iterator-invalidation-rules) to which I secretly know the answer, wait a while and then submit my answer as if I just found it, and read other people's answers too, and this is _perfectly normal_. We're all sharing knowledge here. – Lightness Races in Orbit Jan 08 '13 at 16:59
  • @LightnessRacesinOrbit Oh okay! Thanks for clearing that up! Now I know. :) – SISYN Jan 08 '13 at 17:03
  • @LightnessRacesinOrbit whilst you're correct that it's allowed (perhaps even encouraged) to answer your own questions, and sometimes to "ask" questions knowing full well what the answer is, in this case I believe the OP has done an exceedingly poor job of phrasing his question such that it usefully serves as a "reference answer". It provides nothing that a few seconds on Google wouldn't have found, nor is it written in such a way that future searches on Google would turn up this particular question. – Alnitak Jan 08 '13 at 22:39
  • @Alnitak: _That_ I find hard to dispute. – Lightness Races in Orbit Jan 09 '13 at 02:02
  • Maybe I did a poor job phrasing the question; however, if I did an excellent job of phrasing the question, I wouldn't have needed to ask the question. I only ask questions on Stack Overflow after unsuccessfully researching for a solution. It was suggested that this question was written in a way that it wouldn't show up in future searches but even after 5 years, the long tail of curiosity seems to have people continually clicking on this particular question and hopefully it's helping them. – JustinP May 08 '18 at 15:37
1

The hat sign ^ is not a power operator in PHP; it is an XOR operator.

Therefore $x^$y is valid PHP, but will give you a boolean result, not X to the power Y.

Manual reference here: http://php.net/manual/en/language.operators.bitwise.php

If you want to do power of in PHP, you need to use the pow() function

Hope that helps.

SDC
  • 14,192
  • 2
  • 35
  • 48