Re: [foreach] - is it proper to...
| From: | Tedd Sperling | Date: | Thu, 31 May 2012 17:01:27 +0000 |
| Subject: | Re: [foreach] - is it proper to... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318051@lists.php.net to get a copy of this message | ||
On May 31, 2012, at 12:48 PM, Tristan wrote:
> I'm using Zend Studio and it had a suggestion that I do a foreach as such
>
> foreach($entry as $entry){
>
> }
>
> instead of
>
> foreach($entries as $entry){
>
> }
>
> they both seem to work but, from a readability standpoint and just makes
> more sense to me to use method 2. Is it bad practice to go with the 1st
> method of coding or preferred?
>
> Thanks, T
Let's look at that.
In the first case you're doing a foreach loop on an array entitled $entry (single) using a
variable entitled the same name representing the values in each element of the array -- that
doesn't make sense even if it works or is recommended by Zend Studio. That would be a hell of a
bug to find.
In the second case, I find the syntax more correct. You are doing a foreach loop on an array
entitled $entries (plural) using the values contained in the array as $entry (single) -- that makes
much more sense to me.
My $0.02
Cheers,
tedd