#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2

    Endless while loop... oops?


    The following code gives me a never ending loop of the name "Jeff" being printed endlessly. In the tutorial, however, the result from this code is a list of the names in the array, printed once. What am I doing wrong?
    PHP Code:
    <?php
    $arr 
    = array('Jeff''Collis''Tash''Amanda');

    $i 0;
    while(
    $i 10 ) {
        echo 
    "<li>$arr[$i]</li>";
    }
    ?>
    I'm using this tutorial:
    https://tutsplus.com/course/php-fundamentals/
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Where you're not changing the value of $i within the loop, how can it ever increase to the point where it equals 10 and you exit the loop?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    a while runs as long as the condition is fulfilled. So in your case, the loop runs as long as $i is smaller than 10. Does it ever end? No, because $i is 0 and stays 0. You never change it.

    I guess what you actually wanted to do is increment the counter after every loop iteration:

    PHP Code:
    while ($i 10 ) {
            echo 
    "<li>$arr[$i]</li>";
            
    $i++;        // don't forget this!    

    That's actually the first thing to check when dealing with loops: Is it guaranteed to end?

    Comments on this post

    • thechooch agrees
    The 6 worst sins of security ē How 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. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2
    that's right, oh woah, how could I not have seen this? haha. well, thanks!

    PHP Code:
    $i 0;
    while(
    $i 10$i++) {
        echo 
    "<li>$arr[$i]</li>";

    this would be a simple fix
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Have you actually tested this?
    The 6 worst sins of security ē How 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".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2
    I posted before testing, sorry.

    I figured it'd work like that because in a for loop I used something similar.
    PHP Code:
    for( $i 0$i count($arr); $i++) 
    It does make more sense to do what you showed me, thanks.
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Yes, that's possible. Note, however, that a for loop is basically just a special case of a while loop, so you can do the same thing with a while loop:

    PHP Code:
    $i 0;
    while (
    $i 10 ) {
            echo 
    "<li>$arr[$i]</li>";
            
    $i++;        // don't forget this!    

    The important part is the $i++, which increments the counter after each loop iteration.

    In your case, the most appropriate loop would be the foreach loop, which is used to iterate through the elements of an array (or an enumerable object):

    PHP Code:
    <?php

    $names 
    = array('Jeff''Collis''Tash''Amanda'); 

    foreach ( 
    $names as $name ) {
        echo 
    "<li>$name</li>";
    }
    The for loop is used for any kind of counting. And the while loop is a general-purpose loop.

    In reality, you'd have to escape the names for security reasons, but that's another topic.
    The 6 worst sins of security ē How 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".
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    the Netherlands
    Posts
    41
    Rep Power
    2
    I see, I think I understand. I did some PHP programming a couple of years ago but I lost a lot of the knowledge I had--and it wasn't all that much. Now that I'm trying to relearn it, a lot of things are coming back but I'm learning a lot of new things too. It's fun, though I'm pretty much an absolute beginner... so these answers are really helpful!

IMN logo majestic logo threadwatch logo seochat tools logo