SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessing i+1 inside a for loop

    in the code below, echo $temp_large outputs nothing. is it possible to access i+1 during the first time the loop is running? or can i+1 only be accessed during the second run?

    Code:
    for ($i=1;$i<=6;$i++)
    {
    $temp_thumb = $_FILES['img'.$i]['tmp_name']
    echo "temp_thumb:".$temp_thumb."<br />";
    
    $temp_large = $_FILES['img'.$i+1]['tmp_name'];
    echo "temp_large:".$temp_large."<br />";
    }
    I need someone to protect me from
    all the measures they take in order to protect me

  2. #2
    SitePoint Evangelist stef25's Avatar
    Join Date
    Nov 2004
    Location
    belgium
    Posts
    465
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replacing $i+1 with $++ doesnt work either ... the lines below output 1 and then again 1. how can $i++ also be 1?!

    Code:
    echo "i is: ".$i."<br />";
    echo "i++ is: ".$i++."<br />";
    I need someone to protect me from
    all the measures they take in order to protect me

  3. #3
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    522
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    it looks like $i++ increments $i but does not return the new value. If you follow the code in your last post with
    Code:
    echo "i is now: ".$i."<br />";
    then you get 2, so it has incremented the variable.
    In your code just do $i+1 instead of $i++.
    Anyway, I don't think you really want to increment $i, as that'll mean your loop only runs for numbers 1,3,5 (i think.. but i haven't tried)


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
  •