I'm an idiot stuck in endless loop. This thing almost crashed my pc!

Discussion in 'PHP' started by JasonGreen, Oct 7, 2009.

  1. #1
    Seriously, I'm not that bright. My whole life random stragners have taken pity on me. You only have to meet me once to see I am incapable of even dressing myself. I pass off as hip, but the truth is I just forgot to wear a belt.

    Here is my endless loop. Probably best not to run, it keeps freezing my PC

    $breed = rtrim(strtok($pet_spa, " \n"));
    $appointment = rtrim(strtok(" \n"));
    $day = rtrim(strtok(" \n"));
    $time = rtrim(strtok(" \n"));
    $charge = rtrim(strtok(" \n"));

    printf ("%9s\n", "Breed App. Day Time Charge");

    while($breed){
    echo $breed, " ";
    echo $appointment, " ";
    echo $day, " ";
    echo $time, " ";
    echo "\$" . number_format ($charge,2);
    }

    I have to use While. I know there are other ways of doing this. But can't I use While?
     
    JasonGreen, Oct 7, 2009 IP
  2. kbluhm

    kbluhm Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In your code, $breed is never modified. So if it contains any value equivalent to TRUE, that is where your infinite loop is coming from. You are essentially doing: while ( TRUE ) { echo "Yup, still true.\n"; }

    Perhaps you want your while to be if?
     
    kbluhm, Oct 7, 2009 IP
  3. alphamonk

    alphamonk Peon

    Messages:
    116
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you only want to iterate through the loop once use "if" for logic control. if $breed is initialized, like in the beginning of your script, your echo statements inside the loop will run.

    
    if($breed){
    echo $breed, " ";
    echo $appointment, " ";
    echo $day, " ";
    echo $time, " ";
    echo "\$" . number_format ($charge,2);
    } 
    
    Code (markup):
     
    alphamonk, Oct 7, 2009 IP
  4. JasonGreen

    JasonGreen Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I don't think I can use if or any other variation of it since I'm getting the data from another file (include 'info.php') . Is ther anything I can do to just stop the loop? I need it structured this way to keep the variables for other manipulations
     
    JasonGreen, Oct 7, 2009 IP
  5. alphamonk

    alphamonk Peon

    Messages:
    116
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You are going to have to unset the variable $breed in order to complete the loop.
    The following will work for you, but this is not the real answer you need. If I knew more about what you are trying to accomplish in a larger picture, I could be of more help to you.

    
    while($breed){
    echo $breed, " ";
    echo $appointment, " ";
    echo $day, " ";
    echo $time, " ";
    echo "\$" . number_format ($charge,2);
    $breed = "";
    }
    
    Code (markup):
     
    alphamonk, Oct 7, 2009 IP