alternative for foreach ()

php.internals

Jaap van Ganswijk

22 years ago
Hi all, I'd like to propose an alternative for the foreach statement. Of course the construct: while (list($i,$d)=each($A)) is much harder to read than: foreach ($A as $i=>$d) and although I had doubts at first I'm currently rewriting all my old scripts to the new format whenever I encounter them, but I would have much rather seen that this problem hadn't been solved using a new statement but with a new operator like: while (each $i=>$d in $A) or shorter: while ($i=>$d in $A) I think that my solution to the problem has several advantages: 1. It's possible to use the in-operator as part of a more complex expression like: for (;$d in $A and $t++<10;) //I use this a lot temporarily //in the testing phase of scripts It would even be possible to write something like: while ($d in $D and $e in $E) { $A[]=$d+$e; } 2. Rewriting old code is much easier because the variables stay in the same order. 3. One is free to use a while or a for statement. I wouldn't bring this matter up if it already was 'water under the bridge', but this syntax doesn't conflict with the foreach syntax, so I'm just suggesting that my syntax is added too, so people can choose what they use. It wouldn't surprise me when the foreach syntax would be deprecated soon... Greetings, Jaap

Wez Furlong

22 years ago
> It wouldn't surprise me when the foreach syntax would be > deprecated soon...
If you're trying to humourous, I suggest that you first earn some karma by contributing some real code to the PHP project. Your suggestion will not be implemented because it is redundant and confusing to have yet another way to iterate an array, especially a syntax that doesn't fit with the PHP spirit. --Wez.

Jaap van Ganswijk

22 years ago
At 2003-10-19 13:38 +0100, Wez Furlong wrote:
>> It wouldn't surprise me when the foreach syntax would be >> deprecated soon... > >If you're trying to humourous, I suggest that you first earn >some karma by contributing some real code to the PHP project. > >Your suggestion will not be implemented because it is >redundant and confusing to have yet another way to iterate >an array, especially a syntax that doesn't fit with the PHP >spirit.
Why doesn't it fit the 'PHP spirit'? I think the 'foreach (as)' construct is very foreign to PHP or any C-like language. Algol on which C is based, already allowed the assignment of elements from a set (in the form of a list of expressions) in a for-loop something like this: for x=1,2,5..8 All C-like and indeed almost all higher languages have always had their assignments flowing to the left in an expression. I think it's a mistake to reverse this suddenly for a new construct in PHP, but perhaps you can explain the logic of it to me. Until you do I can't help but seeing it as a 'perl-ification' of PHP. And why be so hostile? I used to write my own compilers, assemblers and linkers and lately I contributed this Amazon webservices script to the world: http://www.chipdir.nl/amazon/ (No it's not big science, but it saves people who are interested in this subject having to plough through miles of Amazon's vague documentation and non-working as-is scripts. And whenever someone criticizes whatever I made I take it very seriously based on what they say and not on their status of cooperator or whatever. Since 1995 I provide a free WWW information site about chips to the world: http://www.chipdir.org/ and from the logs I have seen that (even) employees from all major corporations have been consulting it, so probably at least one piece of hardware you're using (directly or indirectly) has been designed with the use of my site. Perhaps it's next time when you fly a plane... Greetings, Jaap PS. Is there a separate mailing list for discussing design issues of PHP as opposed to implementation issues?