Doc #80296 [NEW]: Migration docs regarding foreach needs examples and clarification
From: php4fan at gmail dot com
Operating system: all
PHP version: Irrelevant
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:Migration docs regarding foreach needs examples and clarification
Description:
------------
---
From manual page:
https://www.php.net/manual/en/migration70.incompatible.php#migration70.incompatible.foreach.by-value
---
It says:
"""
foreach by-value operates on a copy of the array ¶
When used in the default by-value mode, foreach will now operate on a
copy of the array being iterated rather than the array itself. This
means that changes to the array made during iteration will not affect
the values that are iterated.
"""
It is unclear what this means, because what I understand from it is not
true.
Also, an example should be given, with different output for 5.x vs 7.x.
Even if the explanation wasn't ambiguous as it is, an example should
ALWAYS be given for all documented changes.
Test script:
---------------
Consider these scripts:
<?php // EXAMPLE 1
$a = ['x', 'y', 'z'];
foreach ($a as $i => $v) {
$a[$i] = '!!!';
}
print_r($a);
?>
<?php // EXAMPLE 2
$a = ['x', 'y', 'z'];
foreach ($a as $v) {
$a[1] = '!!!';
}
print_r($a);
?>
Expected result:
----------------
In PHP 5, the first example outputs:
Array
(
[0] => !!!
[1] => !!!
[2] => !!!
)
and example 2 outputs:
Array
(
[0] => x
[1] => !!!
[2] => z
)
as expected.
From the confusing statement cited above, I would seem to understand
(and that would surprise me) that the first example in PHP 7 would
output:
Array
(
[0] => x
[1] => y
[2] => z
)
or perhaps, that example 1 would work as in PHP 5, but that example 2
would output
Array
(
[0] => x
[1] => y
[2] => z
)
Actual result:
--------------
Fortunately, that is not the case. But then, I'm completely lost as to
what the sentence means.
I can't figure out an example where the behavior in PHP 5 and 7 would
differ.
--
Edit bug report at https://bugs.php.net/bug.php?id=80296&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=80296&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=80296&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=80296&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=80296&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=80296&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=80296&r=support
Expected behavior: https://bugs.php.net/fix.php?id=80296&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=80296&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=80296&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=80296&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=80296&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=80296&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=80296&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=80296&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=80296&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=80296&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=80296&r=mysqlcfg
Thread (6 messages)
- php4fan at gmail dot com