Req #74502 [Com]: Adding ready variables to foreach

From: Date: Wed, 26 Apr 2017 19:25:41 +0000
Subject: Req #74502 [Com]: Adding ready variables to foreach
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-208810@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 Comment by: spam2 at rhsoft dot net Reported by: most dot talebi at gmail dot com Summary: Adding ready variables to foreach Status: Open Type: Feature/Change Request Package: Variables related PHP Version: Next Minor Version Block user comment: N Private report: N New Comment: 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++; } Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#208810) next »