Page 3 of 3 First 123
  • Jump to page:
    #31
  1. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,236
    Rep Power
    1469
    Originally Posted by jaad
    Now I get it!

    so in other words, what would work would be to write it like this: (==5) right? Amazing! I'm finally learning something. thank you
    Yes, it would work. The script would run without an error, but the loop would not execute.

    Read about Language Operators on PHP.net.

    == is a comparison operator

    = is an assignment operator
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
  2. #32
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    33
    Rep Power
    1
    Originally Posted by Nilpo
    Yes, it would work. The script would run without an error, but the loop would not execute.

    Read about Language Operators on PHP.net.

    == is a comparison operator

    = is an assignment operator
    Let me try to explain this to myself:

    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?
  4. #33
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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:

    PHP Code:
    for ($counter 1$counter <= 10$counter++)
    {
        echo 
    $counter;

    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.

    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

    • Nilpo agrees
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #34
  7. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,236
    Rep Power
    1469
    Originally Posted by Jacques1
    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.
    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.
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
Page 3 of 3 First 123
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo