December 6th, 2013, 11:44 AM
Yes, it would work. The script would run without an error, but the loop would not execute.
Originally Posted by jaad
Read about Language Operators on PHP.net.
== is a comparison operator
= is an assignment operator
December 6th, 2013, 12:46 PM
Let me try to explain this to myself:
Originally Posted by Nilpo
So it wouldn't work because $count has been set to 1 and in order to get to 5 it must go through a loop that will bring it to 5 by adding ++$count at each loop but since my expression does not include a "<", then the loop as nowhere to start or to get to 5 since it can get initialized by increment of 1. Is my logic flawed?
December 6th, 2013, 01:18 PM
This is correct. Simply put: $count is initialized with 1. The loop starts every iteration by checking if $count equals 5. Since it doesn't, the loop immediately stops. That's it.
Two things, though:
Loops that are based on counters should be written as for loops. It looks like this:
As you can see, all the data for the counter is on top: You first initialize the counter variable. In this case, $counter starts at 1. Then you specify the loop condition. In this case, $counter must not exceed 10. And then you define what should happen to the counter variable. In this case, it's simply incremented.
for ($counter = 1; $counter <= 10; $counter++)
When working with counters, for loops are preferable over general while loops, because they're more compact, easier to read and clearer.
Secondly, you should avoid the term "doesn't work". Programming is about being precise. Saying that something "doesn't work" isn't precise at all. It could mean pretty much anything: that the program has crashed, or that the program doesn't do what it should, or that something unexpected has happened, or whatever.
I'm pointing this out because people in this forum often tell us that their code "doesn't work", and then we have a hard time trying to find out what they actually mean. It's better to use precise language.
Comments on this post
December 6th, 2013, 01:47 PM
Adding to this: when the code produces an error, post the exact error message whenever possible. If you're not seeing the error, we can show you how to disable friendly HTTP errors on your development server.
Originally Posted by Jacques1