Page 1 of 1

foreach($blah as $blah) = Bad Practice?

Posted: Sat Aug 08, 2009 9:05 am
by Qphoria
Although it is perfectly legal in PHP to use the same variable as the array and the individual value of a foreach loop. 1 major downside is that it then breaks that array for use further down the page.

I suppose it's not often that you'd use it multiple times in one page. Though one example would be pagination for example. Having it on top and bottom of a page for convenience, if you did foreach ($pages as $pages) then you wouldn't be able to use it after that.

Wouldn't it be best to always be sure to use foreach($pages as $page) ?

Re: foreach($blah as $blah) = Bad Practice?

Posted: Sun Aug 09, 2009 8:42 pm
by JNeuhoff
Why would you ever want to to use something like

Code: Select all

foreach ($pages as $pages)
It doesn't make sense at all to me!

Re: foreach($blah as $blah) = Bad Practice?

Posted: Mon Aug 10, 2009 1:18 am
by Qphoria
I don't. It was something Daniel used in 0.x that I changed... but I am seeing he uses it in 1.x now as well. So I was wondering why.

Take a look at any file in catalog/view/theme/default/template/product/*.tpl

They all have

Code: Select all

foreach ($sorts as $sorts) {

Re: foreach($blah as $blah) = Bad Practice?

Posted: Mon Aug 10, 2009 7:03 am
by Daniel
you are right!

I only did it with a few names.

its hard when you have one var called page that gives the page number and another called pages that holds all the page numbers.