|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-04-26 19:15 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 09:00:01 2025 UTC |
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++; }