For nearly two decades, if you include precursor PHP/FI, PHP lacked a basic mathematical operator, the one for exponentiation. According to the author of the rant heard ‘round the world bashing PHP:
“There is no exponentiation operator, only the pow function.”
Eevee (see “PHP: a fractal of bad design“)
Up to this year, the only argument I could make regarding Eevee’s statement is that additionally, the bcmath and gmp extensions each offer a pow-style function. However, history has intervened, rendering his assessment as untrue, effective as of PHP5.6.
A member of the original PHP Group 12 years ago made the following pronouncement with respect to adding a pow operator:
“not going to happen. just suck it up and use pow()”
firstname.lastname@example.org, 2002 (see bugs.php.net/bug.php?id=13756)
Winstead’s prediction held true for years as Userland diligently followed his advice — until recently. Why? Maybe there was something in the air 🙂 During 2012 and 2013, I gave variations of a talk about PHP’s numeric quirks at CodeWorks, OCPHP Meetup, SCALE’s Birds of a Feather, and later at OSCON followed up by a talk for the Web and PHP Conference. In each presentation, I mentioned the peculiar situation of PHP lacking a pow operator and contrasted that deficit with what Python offers a developer, even tho’ my loyalties are with PHP.
All of a sudden in November 2013, Tjerk Meesters put forth an RFC proposing an exponent operator for PHP, consisting of the double asterisk (**) symbol which follows other languages, such as Python, Perl and Ruby. Did my campaign for an exponent operator have any bearing? Who knows? The most reasonable conclusion is that the time had simply come for this operator to make its debut in PHP.
When voting commenced, the outlook seemed to change hourly with victory seeming assured and then doubtful. We in Userland while mostly unable to vote, could at least urge all who were eligible to vote. Note, this particular RFC required a 2/3 majority for the proposal to pass. So, I respectfully asked via this post that *all* of those with voting privileges vote!
I hoped that shortly after the New Year arrived PHP would join the modern age and finally have an exponent operator! But some worried about whether this addition would ultimately benefit PHP and/or Userland, fearing that the new operator might lead to needless confusion. See discussion at Reddit. The concerns are valid and so I’d like to explore them. Here’s what you need to know:
** does not function identically as pow() in all cases. For that reason, you may prefer at times to continue using pow(). For example, consider the following:
<?php echo pow(-3,2); // 9 echo -3 ** 2; // -9 $a = -3; echo $a ** 2; // 9
Confused? I admit that initially I felt perplexed. Then, I learned that the RFC largely observes the way that operator functions in Perl, Python, and Ruby (see example code of each at http://codepad.org/uos1LfSS, http://codepad.org/sZGriaYV, and http://codepad.org/pGj14lEd).
The first example using ** is in effect the equivalent of writing:
0 – (3 ** 2)
The ** has a higher precedence. PHP’s so-called unary minus is really an operation involving a number being subtracted from 0.
The next example using ** has a different outcome because of the variable assignment. Thus here’s its equivalent:
(-1 * 3) ** 2 == -3 * -3 // 9
Or (0 – 3) * (0 -3) // 9
The unary minus in this second example clearly has a smaller reach which results in a positive result.
If you find working with negative values too confusing, you may of course use braces as follows:
<?php print (-3) ** 2; // 9
Basically, you need to forget what you learned in algebra and think instead in computer terms, specifically operator precedence and binding.
What truly confounds is code like this:
<?php echo 0 ** 0; // 1 echo pow(0,0); // 1
The odd result relates to an ongoing mathematical debate over how to comprehend 0 to the power of 0. One may argue that the results are undefined. Others argue that it’s convenient to have the result equal to 1. See http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_power_of_zero.
If you wonder whether PHP really needs yet another operator, ask yourself, which code you would rather read/write:
<?php echo pow(2,3); // 8 echo 2 ** 3; // 8
The latter involves a difference of only two fewer key strokes but its form is certainly much more legible and its meaning is immediately apparent.
Of all the RFC’s I’ve followed, this one progressed dramatically as my updates below indicate:
Update – Dec 22, 2013:
This vote is like a roller coaster. Meesters opted to close the vote and revise the RFC since the Father of PHP upon closer inspection noticed that it included a pow construct. The problem with that feature is that it would interfere with the perfectly good pow() which hardly requires replacement. So, it sure seemed that the RFC would revert to “Discussion” status with voting happening at some unspecified time in 2014. But, according to the RFC’s change log the voting is on again as of Dec 22nd. Well, that’s probably according to European time. Look at the vote again and see who is coming on board with this proposal. There is hope! Yay!
Update – January 6, 2014:
One of the core contributors wants to extend the deadline for voting, but the RFC’s author rejects that idea, noting that the voting period was three weeks long instead of the usual two. So, I’m happy to report that Meesters has stated that PHP is going to have a new exponent operator as of PHP5.6 (see http://markmail.org/message/7ny7z6zts6sca6tb). The vote was more than the required 2/3 majority.
At one point during the voting, I noticed that the individual votes suddenly disappeared, the only number visible being the total vote tally, a disconcerting situation which caused concern even among various core contributors. (Other active RFC’s were similarly effected.) This incident raises questions about what precisely happened and why as well as how such a situation enfolded — but that is the subject for another post. In the meantime, let’s celebrate that what some deemed as impossible miraculously became possible. The ** operator will appear in PHP (version 5.6). This seemingly small change will be a welcome addition, making it ever so much more pleasurable for users to work with exponents.