Req #74502 [Opn->Wfx]: Adding ready variables to foreach
| From: | requinix@php.net | Date: | Wed, 26 Apr 2017 19:29:32 +0000 |
| Subject: | Req #74502 [Opn->Wfx]: Adding ready variables to foreach | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-208811@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74502&edit=1
ID: 74502
Updated by: requinix@php.net
Reported by: most dot talebi at gmail dot com
Summary: Adding ready variables to foreach
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Variables related
PHP Version: Next Minor Version
Block user comment: N
Private report: N
New Comment:
Why not? Because you can get $i from the iteration key (for simple lists) or two tiny lines of code
(as pointed out). Because $is_first is equally trivial to implement. Because $is_last often cannot
be determined at all.
Because this is all unnecessary overhead and added complexity for the 9/10 times developers
don't need it.
Previous Comments:
------------------------------------------------------------------------
[2017-04-26 19:25:36] spam2 at rhsoft dot net
frankly you are confusing PHP as programming language with a framework and there is no need to bloat
up the core with every thinkable usecase just to save 2 lines of trivial code
is it really that hard even in combiantion with count($list_of_users)
$i = 0;
foreach($list_of_users as $user)
{
$i++;
}
------------------------------------------------------------------------
[2017-04-26 19:15:00] most dot talebi at gmail dot com
Description:
------------
Why PHP does not provide some basic and ready variables for foreach-loops? A variable, for example,
for holding the iteration count. Because foreach-loops are an abstract way of looping (than basic
for-loop), hence it could be way better to provide at least two (or event one) variables for
foreach-loops which updates on every iteration.
Test script:
---------------
For example, the following can save the life of PHP programmers:
foreach($list_of_users as $user) : $i
{
// the variable of $i updates on every iteration to
// reflect the current iteration count
}
Or for more easier programming, a second variable can be added to reflect $is_last status:
foreach($list_of_users as $user) : $i, $is_last
{
// the variable of $i updates on every iteration to
// reflect the current iteration count
// $is_last is true if the iteration is the last iteration, and is
// always false for all other previous iterations.
}
// the same case can even be true for $is_first variable
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74502&edit=1