> On 20 Nov 2014, at 09:34, Rowan Collins <rowan.coll...@gmail.com> wrote:
> 
> Note that there are many, many places where expressions are actioned for 
> their side effects even though the result is discarded. The following will 
> increment $a 3 times, even though it is discarded each time:
> 
> $a = 0;
> ++$a; // 1
> class Foo { function __construct() {} }
> new Foo(++$a); // 2
> function bar() {}
> bar(++$a); // 3
> class WTF {}
> new WTF(++$a); // suddenly, PHP becomes lazy
> 
> Test: http://3v4l.org/tcnv63 (note that HHVM increments all 4 times; PHP 4 
> only twice because class Foo has no old-style constructor)
> 
> Lazy evaluation certainly has benefits, but it's not, in general, a feature 
> of PHP. That it happens in this one very specific case is what is so 
> unexpected.

You’re forgetting perhaps the most common and fundamental case in PHP (and C, 
from which it comes): expression statements.

Any expression, followed by a semicolon, is a valid statement. The expression’s 
result is discarded, but it’s always evaluated. Otherwise function calls 
wouldn’t work, nor would assignment, both of which are expression statements.

--
Andrea Faulds
http://ajf.me/





--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to