SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2000
    Location
    North Central AR
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't figure out why my "for" loop will not stop looping even after the array is empty.

    $cnt = count($pets_array);

    for ($id = 0; $id < $cnt; ++$id) {
    include ("$pets_array[$id]" . ".php");
    }

    The array elements come from a form. Each input is has the name "pets_array[] and each have their own value. The valuse is passed to the array and the .php is added then that script is run. When I chose one of the choices the script runs fine, but I keep getting the error :

    Warning: Failed opening '.php' for inclusion (include_path='')

    over and over until i stop it. I've looked over the code many times. What am I missing?

    ronnie
    The largest selection of PLR Articles on the Planet!
    Many Private Label Rights Products.

  2. #2
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of: ++$id
    Do: $id++

  3. #3
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also you don't need the first bit of your include thingy in quotes (although that won't stop it working):

    include ($pets_array[$id] . ".php");

    Just makes less work for the PHP engine as it doesn't have to evaluate the quotes. In fact an even better line would be:

    include ($pets_array[$id] . '.php');

    With single quotes as the .php string doesn't need to be parsed for variables. To be honest though the performance increase is completely negible - it's just something I've started doing recently as good coding practise.

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pre-increment versus post-increment

    Originally posted by ronnie
    I can't figure out why my "for" loop will not stop looping even after the array is empty.

    $cnt = count($pets_array);

    for ($id = 0; $id < $cnt; ++$id) {
    include ("$pets_array[$id]" . ".php");
    }
    With the above code you should have also found that the fisrt element of the array was also skipped by the for loop.

    ++$id <- pre increment $id
    Pre-increment works like this. Take the current value of $id and add one to it and store that new value in $id and *then* use that new value in the statement.

    $id++ <- post increment $id
    Post-increment works like this. Take the current value of $id and use that value in the current statement, and then after that, add one to the current value if $id and store the new value back into $id.

    So the first time through your for loop the logic would have worked like this (assuming there are > 0 elements in the array):
    - initialize the loop counter to 0.
    - evaluate the for condition - 0 is less than array length is true - so the for loop block will be entered.
    - Pre-increment $id
    - execute the statements in the for loop.

    However, using post increment, the value of $id will not be incremented until *after* the statements in the for loop have been executed.

    Well, I think thats something like how it works
    Last edited by freakysid; Apr 11, 2001 at 18:01.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2000
    Location
    North Central AR
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone for the response. I changed the ++'s to the other side and it works perfectly. Thanks freaky for the explanation, that was going to be my next question.. I also changed the code as skunk suggested, nothing wrong with writing good code.

    Thanks again!

    ronnie
    The largest selection of PLR Articles on the Planet!
    Many Private Label Rights Products.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •