LWN: Comments on "What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard)" https://lwn.net/Articles/639000/ This is a special feed containing comments posted to the individual LWN article titled "What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard)". en-us Tue, 16 Sep 2025 14:29:22 +0000 Tue, 16 Sep 2025 14:29:22 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639477/ https://lwn.net/Articles/639477/ nybble41 <div class="FormattedComment"> That was supposed to be a=1, b=2, c=3. I fixed the text after previewing, but must have hit "Publish" rather than "Preview" (thus submitting the original version). A simple JS OK/Cancel warning on the "Publish" button when the text is out of sync with the preview would have been nice... with no impact to those without JS.<br> </div> Wed, 08 Apr 2015 01:21:55 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639467/ https://lwn.net/Articles/639467/ juliank <div class="FormattedComment"> B and C are both 2 in your example, so b=c, not b&lt;c.<br> </div> Tue, 07 Apr 2015 22:46:59 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639457/ https://lwn.net/Articles/639457/ nybble41 <div class="FormattedComment"> <font class="QuotedText">&gt; e.g. iff a &lt; b and b &lt; c, then (a &lt;=&gt; b) &lt; (b &lt;=&gt;c).</font><br> <p> That isn't true. Consider the trivial case where a=1, b=2, and c=2, so a &lt; b and b &lt; c. (a &lt;=&gt; b) and (b &lt;=&gt; c) are both -1, so you have -1 &lt; -1, which is false.<br> </div> Tue, 07 Apr 2015 22:31:40 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639454/ https://lwn.net/Articles/639454/ mathstuf <div class="FormattedComment"> The `Ordering` enumeration in Haskell is also of type `Ord`:<br> <p> Prelude&gt; :t LT<br> LT :: Ordering<br> Prelude&gt; :t LT &lt; GT<br> LT &lt; GT :: Bool<br> </div> Tue, 07 Apr 2015 22:11:35 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639366/ https://lwn.net/Articles/639366/ niner <div class="FormattedComment"> "This could be fixed by adding enumerations to the language and returning LT, EQ, GT values."<br> <p> And indeed, it has been fixed already in Perl 6:<br> <p> <font class="QuotedText">&gt; perl6 -e 'say 1 &lt;=&gt; 2'</font><br> Less<br> <font class="QuotedText">&gt; perl6 -e 'say 2 &lt;=&gt; 1'</font><br> More<br> <font class="QuotedText">&gt; perl6 -e 'say (1 &lt;=&gt; 2).WHAT'</font><br> (Order)<br> <p> That PHP is copying a feature of Perl 5 (as clearly indicated by the name of the operator) but not look at how Perl 6 does it better is just...<br> </div> Tue, 07 Apr 2015 14:52:20 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639351/ https://lwn.net/Articles/639351/ nix <div class="FormattedComment"> I shouldn't write comments when flued up.<br> <p> I meant to say, "the &lt;, = and &gt; comparators applied to the return value of &lt;=&gt; produce the same results as the same comparators would applied to the arguments to the corresponding calls of &lt;=&gt;". i.e. they preserve the same relative ordering.<br> <p> e.g. iff a &lt; b and b &lt; c, then (a &lt;=&gt; b) &lt; (b &lt;=&gt;c).<br> <p> You cannot do this with constants (unless you overload the &lt; = &gt; operators to do the appropriate things with those constants, probably by setting the constants to be equivalent to -1, 0, and +1).<br> <p> </div> Tue, 07 Apr 2015 13:49:22 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639148/ https://lwn.net/Articles/639148/ HelloWorld <div class="FormattedComment"> <font class="QuotedText">&gt; It's not efficiency gains. It's that the &lt;, = and &gt; comparators applied to the return value of &lt;=&gt; produce the same results as the same comparators would applied to &lt;=&gt;'s argument.</font><br> I have no idea what you're trying to say here. &lt;, =, &gt; and &lt;=&gt; are all binary operators, you can't apply them to single values.<br> <p> <font class="QuotedText">&gt; You can't get that rather useful duality with a bunch of named enumerations, unless you have an ordering over them,</font><br> Haskell's Ordering datatype does.<br> <p> <font class="QuotedText">&gt; and if you do, why not use -1, 0, 1 in any case?</font><br> Well, if your compiler does exhaustiveness checking then it's handy. In PHP it doesn't seem too useful, but then nothing in PHP does.<br> </div> Sun, 05 Apr 2015 21:57:07 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639147/ https://lwn.net/Articles/639147/ nix <div class="FormattedComment"> It's not efficiency gains. It's that the &lt;, = and &gt; comparators applied to the return value of &lt;=&gt; produce the same results as the same comparators would applied to &lt;=&gt;'s argument. You can't get that rather useful duality with a bunch of named enumerations, unless you have an ordering over them, and if you do, why not use -1, 0, 1 in any case?<br> <p> It's not as if "two numbers a unit distance on either side of zero" is a difficult concept, or one that'll need to change when new sorts of number turn up (unless, I suppose, it's extended to deal with complex numbers, and in that case the new return values are obvious: -i, i, etc.)<br> </div> Sun, 05 Apr 2015 21:20:09 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639141/ https://lwn.net/Articles/639141/ nybble41 <div class="FormattedComment"> <font class="QuotedText">&gt; You can either implement compare ... or (&lt;=) which returns Bool.</font><br> <p> You also need to implement (==), since Eq is a prerequisite for Ord. But yes, either `compare` or (&lt;=) is sufficient, along with an Eq instance. There remains an expectation (in `compare` and elsewhere) that two Ord values are exactly one of less-than, equal, or greater-than. It's bad form to leave some comparisons undefined (a partial order) or to implement (&lt;=) and (&gt;=) such that `a &lt;= b &amp;&amp; b &lt;= a &amp;&amp; not (a == b)`.<br> </div> Sun, 05 Apr 2015 19:32:45 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639138/ https://lwn.net/Articles/639138/ mbunkus <div class="FormattedComment"> In languages that support tuples, sure. PHP isn't one, neither is Perl, so in those languages dropping the comparison operator wouldn't make as much sense as it does it Python (or the other way around: introducing &lt;=&gt; makes sense because, unlike Python, PHP doesn't support tuples).<br> </div> Sun, 05 Apr 2015 12:58:42 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639137/ https://lwn.net/Articles/639137/ ABCD <div class="FormattedComment"> To sort by three different things, you can create a turple as your sort key. Turples with the same length sort first by their first element, then by the second, etc.<br> </div> Sun, 05 Apr 2015 12:54:17 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639136/ https://lwn.net/Articles/639136/ HelloWorld Your example is trivial to solve since tuples are compared lexicographically by default. <code>sorted(addresses, key = lambda a: (a.name, a.city, a.street))</code> does the trick and is way shorter than <code>sorted(addresses, cmp = lambda x, y: cmp(x.name, y.name) or cmp(x.city, y.city) or (x.street, y.street))</code>. Sun, 05 Apr 2015 12:52:22 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639135/ https://lwn.net/Articles/639135/ mbunkus <div class="FormattedComment"> If I understand you correctly you mean that first a value »cmpkey« is calculated for each item and then the Python libraries themselves compare those calculated values »cmpkey« in order to determine the order for the elements to sort? Perl's List::MoreUtils package has something simlar (»sort_by«).<br> <p> However, this approach has its own drawbacks. Often enough you cannot really find a projection from the elements to such cmpkey values that's unambiguous, or even fast. Let's take the example of sorting address records. You want to sort by name first, next by city and by street name last. In such cases you would need »cmpkey« to be some kind of concatenation of all three properties – and the concatenation character itself must not occur in the properties either!<br> <p> In such cases calculating the keys is rather expensive. Directly comparing the elements like done in traditional sorting routines is way more efficient.<br> <p> Use the right tool for the job. Both have their uses, so I'm rather surprised to hear that cmp() has been removed from Python 3. I probably don't see the whole picture here, but it seems rather short-sighted.<br> </div> Sun, 05 Apr 2015 08:23:01 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639126/ https://lwn.net/Articles/639126/ mathstuf <div class="FormattedComment"> The main problem I have is that the code now has context all over the place. With an enumeration, the context is exactly where you need it: where it is used. I've seen folks mix up 0, 1, and 2 for stdin, stdout, and stderr. The _FILENO constants are much easier to decipher and make it easier to spot errors in isatty calls.<br> </div> Sat, 04 Apr 2015 23:22:28 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639120/ https://lwn.net/Articles/639120/ vstinner <div class="FormattedComment"> <font class="QuotedText">&gt; Having the "&lt;=&gt;" operator return numbers is a bad design decision IMO. I hope there are named constants for -1, 0, and 1 to indicate the meaning, rather than using magic constants.</font><br> <p> I was very surprised to see the addition of this operation, since Python decided to *remove* this operator in Python3! cmp() of Python 2 had corner cases with None or float("nan"). For example, list.sort() got a new "key" parameter which is easier to use (you compute the key, you don't compare it directly), and more efficient if I understood correctly.<br> </div> Sat, 04 Apr 2015 22:26:45 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639119/ https://lwn.net/Articles/639119/ vstinner <div class="FormattedComment"> <font class="QuotedText">&gt; Did they give sensible names[1] for all of their tokens finally too?</font><br> <p> Sure, PHP 7 gets a new T_SPACESHIP operator...<br> </div> Sat, 04 Apr 2015 22:22:34 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639110/ https://lwn.net/Articles/639110/ tjc <div class="FormattedComment"> <font class="QuotedText">&gt;&gt; Having the "&lt;=&gt;" operator return numbers is a bad design decision IMO.</font><br> <p> <font class="QuotedText">&gt; I don't really agree with that.</font><br> <p> Nor do I. It's commonly held belief that using literals is bad design, but I don't recall having ever seen an error in code caused by the practice. I've seen fabricated examples of possible errors, but nothing in actual production code. I'm sure it does happen, but apparently very infrequently.<br> <p> </div> Sat, 04 Apr 2015 14:54:06 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639103/ https://lwn.net/Articles/639103/ mchapman <div class="FormattedComment"> <font class="QuotedText">&gt; Having the "&lt;=&gt;" operator return numbers is a bad design decision IMO.</font><br> <p> I don't really agree with that.<br> <p> I do wish they hadn't specified this operator in terms of the particular integers -1, 0 and 1 though. It would make much more sense for it to be just "a number less than zero", "zero" and "a number greater than zero" respectively. It shouldn't matter what the number is, just that it falls within one of these three domains.<br> <p> The problem with -1, 0, 1 is that it can lead programmers into thinking that their *own* comparison functions need to have the same behaviour, and so they sully their code with unnecessary logic to make sure only these particular values are returned. But PHP functions like usort(), which take a comparison function, don't require these three particular values.<br> </div> Sat, 04 Apr 2015 10:44:34 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639102/ https://lwn.net/Articles/639102/ deepfire <div class="FormattedComment"> So you value questionable[1] efficiency gains over safety -- noted.<br> <p> We have different values.<br> <p> --<br> 1. It's questionable, because the compiler ought to optimize such trivialties for you.<br> </div> Sat, 04 Apr 2015 10:22:56 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639098/ https://lwn.net/Articles/639098/ HelloWorld <div class="FormattedComment"> <font class="QuotedText">&gt; I don't understand how returning LT, EQ, GT strings is any better than -1, 0, 1.</font><br> They're not supposed to be strings but members of a custom type. <br> </div> Sat, 04 Apr 2015 08:46:02 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639096/ https://lwn.net/Articles/639096/ HelloWorld <div class="FormattedComment"> Actually you can do it both ways in Haskell. You can either implement compare (which returns the Ordering datatype) or (&lt;=) which returns Bool.<br> </div> Sat, 04 Apr 2015 07:57:45 +0000 <=> ??? https://lwn.net/Articles/639090/ https://lwn.net/Articles/639090/ mbunkus <div class="FormattedComment"> Complex sorting by several fields at once. Think of sorting address records. First you compare the name; if those are equal compare the street; if those are equal compare the city etc. In Perl this is trivial to express by using the fact that &lt;=&gt; (or »cmp« which does the same thing on strings instead of numbers) returns 0 for equality.<br> <p> @sorted = sort { ($a-&gt;{name} cmp $b-&gt;{name}) || ($a-&gt;{street} cmp $b-&gt;{street}) || ($a-&gt;{city} cmp $b-&gt;{city}) } @records;<br> <p> If those lines become too long just wrap them properly (kind of hard to do here in the comments). Sure you can express that your way, too, but it becomes that much more verbose. Yes, of course this is a matter of style and preference etc. Use what works for you.<br> </div> Sat, 04 Apr 2015 07:16:43 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639089/ https://lwn.net/Articles/639089/ dottedmag <div class="FormattedComment"> Haskell has that because Haskell has first-class sum types. PHP would require a whole new type in a language to deal with &lt;=&gt; return value. Way too overkill.<br> <p> </div> Sat, 04 Apr 2015 07:08:36 +0000 <=> ??? https://lwn.net/Articles/639088/ https://lwn.net/Articles/639088/ odie The idea is that <em>a</em> and <em>b</em> might be complex objects that reuire a non-trivial amount of processing to compare. In your code, that comparison is run three times, with <em>strcmp</em> in C it need only be run once. In a high-level language, my first thought was that it may make more sense to add caching of the comparison result in the interpretor so that your code example would only run the comparison once anyway. Sat, 04 Apr 2015 07:06:23 +0000 <=> ??? https://lwn.net/Articles/639083/ https://lwn.net/Articles/639083/ gerdesj <div class="FormattedComment"> OK - I am not a programmer, just a sysadmin who has to dabble in PHP/Python/Perl/BASH/Ruby etc at times.<br> <p> Why is this beast a good thing? I read the article and can see that the new operator is a simplification for the given example. However, I would habitually do something like this in any situation that requires comparison:<br> <p> if a &lt; b then do stuff1 <br> else if a = b then do stuff2 <br> else if a &gt; b then do stuff3<br> else do stuff4<br> <p> It might be a bit long winded but very explicit on what is going on and easy to read by anyone. The final else clause picks up invalid comparisons.<br> <p> Any reason why I should change this approach?<br> </div> Sat, 04 Apr 2015 01:17:20 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639082/ https://lwn.net/Articles/639082/ vonbrand <p>If you want to determine the order of two elements, the &lt;=&gt; gives you that with one operation, not two. Could be relevant if comparison is costly. And calling the result &lt; 0, = 0, &gt; 0 à la strcmp(3) in C or -1, 0, 1 à la Perl, is no big deal. Adding some funky enumeration type you then have to compare against in a separate operation just obfuscates the code.</p> Sat, 04 Apr 2015 01:06:26 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639078/ https://lwn.net/Articles/639078/ juliank <div class="FormattedComment"> I'm mostly programming in C, I completely get this, thanks.<br> <p> The values make sense for a lower-level language, but a higher-level language should abstract from raw values and use names instead. Just like they have a boolean type with true/false values instead of just using 1 and 0.<br> <p> The operator is perfectly usable, it's just not the best design possible.<br> </div> Fri, 03 Apr 2015 22:28:22 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639076/ https://lwn.net/Articles/639076/ leifbk <div class="FormattedComment"> It's nice that type checking (or "scalar type hints") is finally coming to PHP. I'm surely going to make use of it.<br> </div> Fri, 03 Apr 2015 22:24:11 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639077/ https://lwn.net/Articles/639077/ bmur <div class="FormattedComment"> I don't understand how returning LT, EQ, GT strings is any better than -1, 0, 1. It's something you learn about the language once and move on.<br> <p> Have you never worked in the C language before? Because the return values to this new operator make perfect sense to me.<br> <p> </div> Fri, 03 Apr 2015 22:22:21 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639075/ https://lwn.net/Articles/639075/ juliank <div class="FormattedComment"> Sorry, yes, I was wrong with that. Still applies to Python, though.<br> <p> The named approach used by Haskell still looks better than just using numbers and is cleaner on the type level.<br> </div> Fri, 03 Apr 2015 22:13:24 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639070/ https://lwn.net/Articles/639070/ nybble41 <div class="FormattedComment"> <font class="QuotedText">&gt; Saner languages like Haskell and Python define ordering (and equality) in terms of less-than and equals relations, which is closer to the mathematical definition.</font><br> <p> Haskell defines the `Ord` typeclass—`compare`, `(&lt;=)`, `(&lt;)`, `(&gt;)`, and `(&gt;=)`—in terms of the `Ordering` data type: `LT`, `EQ`, or `GT`. Implementers of `Ord` are expected to provide a total order, not just less-than and equals; it's basically the same idea as `(&lt;=&gt;)` in PHP or `strcmp()` in C, though without the magic numbers. (They're not all that magic, though: `a &lt; b` becomes `cmp(a, b) &lt; 0`, `a &gt;= b` is `cmp(a, b) &gt;= 0`, etc. I avoid testing for -1 or 1 directly.)<br> </div> Fri, 03 Apr 2015 21:31:10 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639054/ https://lwn.net/Articles/639054/ mathstuf <div class="FormattedComment"> Did they give sensible names[1] for all of their tokens finally too?<br> <p> [1]<a href="https://stackoverflow.com/questions/2588298/whats-the-meaning-of-the-php-token-name-t-paamayim-nekudotayim">https://stackoverflow.com/questions/2588298/whats-the-mea...</a><br> </div> Fri, 03 Apr 2015 19:18:25 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639053/ https://lwn.net/Articles/639053/ juliank <div class="FormattedComment"> Perl and C are not languages too look at when looking at a good language design. Saner languages like Haskell and Python define ordering (and equality) in terms of less-than and equals relations, which is closer to the mathematical definition.<br> <p> cmp() operations are a good idea in some cases (like string comparisons), but they are a bad idea from a language design point of view, because the result does not have an inherent meaning, but needs to be interpreted (it's some sort of magic value).<br> <p> This could be fixed by adding enumerations to the language and returning LT, EQ, GT values. But even then, an operator returning a value from an unrelated enumeration would look weird in a modern programming language.<br> <p> </div> Fri, 03 Apr 2015 19:06:59 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639052/ https://lwn.net/Articles/639052/ vonbrand <p>That particular operator comes directly from Perl, essentially filched from C's strcmp(3)<p> Fri, 03 Apr 2015 18:53:00 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639041/ https://lwn.net/Articles/639041/ juliank <div class="FormattedComment"> Well, most of the stuff they have does not make sense, but adding more of that makes even less sense.<br> <p> I am astonished that they managed to add an AST in PHP 7.<br> </div> Fri, 03 Apr 2015 16:15:27 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639038/ https://lwn.net/Articles/639038/ torquay <ul> <i>... is a bad design decision IMO</i> </ul> <p> Because <a rel="nofollow" href="http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/">everything else in PHP</a> makes sense? </p> Fri, 03 Apr 2015 16:11:39 +0000 What to Expect When You're Expecting: PHP 7, Part 1 (Engine Yard) https://lwn.net/Articles/639037/ https://lwn.net/Articles/639037/ juliank <div class="FormattedComment"> Having the "&lt;=&gt;" operator return numbers is a bad design decision IMO. I hope there are named constants for -1, 0, and 1 to indicate the meaning, rather than using magic constants.<br> </div> Fri, 03 Apr 2015 15:56:37 +0000