Should this be in the UPGRADING notes?

> On 10.11.2015, at 20:45, Nikita Popov <nikita....@gmail.com> wrote:
> 
> On Tue, Nov 10, 2015 at 5:48 PM, Philip Hofstetter <
> phofstet...@sensational.ch> wrote:
> 
>> Hi,
>> 
>> I'm having a cause of slightly ugly code that runs differently from PHP 5.6
>> to PHP 7 and I don't think this difference is documented, nor expected. As
>> I'm not sure whether this is an intended change or not, I'm asking here
>> firstr before making a bug report.
>> 
>> The code in question is
>> 
>> ----
>> $b = [1, 2, 3];
>> 
>> foreach($b ?: [] as &$v){
>>    $v = $v*2;
>>    unset($v);
>> }
>> 
>> var_dump($b);
>> ----
>> in PHP7, this produces
>> 
>> array(3) {
>>  [0] =>
>>  int(1)
>>  [1] =>
>>  int(2)
>>  [2] =>
>>  int(3)
>> }
>> 
>> whereas in PHP 5.6 and earlier, this produces
>> 
>> array(3) {
>>  [0] =>
>>  int(2)
>>  [1] =>
>>  int(4)
>>  [2] =>
>>  int(6)
>> }
>> 
>> what scares me about this is that in order to get the old behaviour in PHP7
>> back, you just have to get rid of the ?: operator:
>> 
>> foreach($b as &$v){
>> 
>> is fine
>> 
>> foreach($b ?: [] as &$v){
>> 
>> is not.
>> 
>> I guess this is related to
>> 
>> http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.foreach.by-value
>> or
>> 
>> http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.foreach.by-ref
>> ,
>> but I'm not entirely sure whether this is actually intended behaviour -
>> it's certainly unexpected to me that applying ?: now always makes a copy
>> whereas before it didn't.
>> 
> 
> This is a bug in PHP 5, which has been fixed in PHP 7 as a side-effect of
> other changes. The new behavior is correct. This issue is tracked at
> https://bugs.php.net/bug.php?id=70857.
> 
> Nikita


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

Reply via email to