Somehow, somewhere the loop count information needs to be handled. A while loop, I assume and have so heard, is faster than a for loop per se since it does not intrinsically have that overhead. What you are describing is a for loop, and if you want to achieve that with a while loop you need to add functionality to the while construct which will give it that overhead, as well. And I really doubt that something rather whimful like that would be better optimized than the established for loops already are when it comes to achieving that task.
Personally I practically never use for loops though.
Anthony, I think there’s no way other than to use a function in some way. Why can’t you just check to see if the var isset and then unset it when you’re done with it if your worried about impacting other code?
Naturally. I think what Anthony is gunning at is he’s got some code he wants to be able to run without affecting or being affected by other PHP scripts. The only way to be sure of this is to avoid making any global vars and using a namespace or psuedo namespaces.
One thing I find irksome about PHP is that namespaces have no effect on the variables - it applies only to functions and constants. This works differently from all other languages that use namespaces and is flat out a bad design decision by the PHP lead team in my opinion.
But Anthony was also asking how to do this without PHP 5.3. As far as I know these are the only two options.
Use a static class as a psuedo-namespace.
class myVars {
public static $counter = 0;
}
myVars::$counter = 0;
while (myVars::$counter < 11) {
//do something
myVars::$counter++
}
Get creative with create_function and call_user_function.
This will get messy fast. Using file_get_contents to get the code from a file will ease the pain somewhat, but it will need to be tag less because of the odd manner of its inclusion. That will play head games with most IDE’s.
I agree entirely but, even after a good nights sleep, I still find it strange that there isn’t something native to do this. How this would be implemented though is another issue, but after a quick scan of some other languages none seem to support this either.
Having to declare a variable ‘just’ for the purposes of creating a loop seems wrong to me, almost messy.
“That’s just the way it is” is not going to appease me at this point. Should this be a userland variable, I personally don’t think so.
Not taking no for an answer only makes it a nice exercise, you should not use this in production. Now, I still highly doubt the practical benefit of this functionality: though I loop using $i or $n, I tend to use $i and $n inside the loop a lot.
I came up with an approach similar to webaddictz’s loop() function (though, his coding style is painful to read); using debug_backtrace() to create an identifier (mine also took the loop number into account) for each individual loop.
Oh no, that’s not my usual coding style: just writing the code that way to make absolutely sure everyone knows that it’s rubbish and should never ever be used If I were to write the code in more than five minutes, it would probably turn up like this:
How would you take the loop number into account? I can’t seem to differentiate between the outer and inner loop, apart from using debug_backtrace. That doesn’t provide the number of loops though, nor the column it’s called from? I’d love to see your hacking, even if it is only to critisise on your coding style
Figured you might catch up on that. Still, I think the effort was nice.
I find it funny how in order to avoid a global variable, all the solutions create tons of variables in the scope of the functions being called.
Isn’t that defeating the original purpose completely (I know those variables aren’t global)?
I haven’t encountered a loop where I had to loop x times without needing the counting variable in some way.