why i am getting 1 string 8 times in for loop

Discussion in 'PHP' started by ironmankho, Nov 18, 2011.

  1. #1
    i am trying to expolde string but why i get 8 times result .... please tell me how i can solve this
    (please not give me suggstion about using for each loop i am already know that)

    <?php
    $a='Madhuri Dixit,Konkona Sen Sharma,Kunal Kapoor,Divya Dutta,Ranvir Shorey,Vinay Pathak,Jugal Hansraj,Yashpal Sharma,Raghuveer Yadav';
    $pieces = explode(",", $a);
    $letter_count = substr_count($a, ',');

    for ($i=1; $i<=$letter_count; $i++)
    {
    $pieces = explode(",", $a);
    echo $pieces[0].'</br>';
    echo $pieces[1].'</br>';
    echo $pieces[2].'</br>';
    echo $pieces[3].'</br>';
    echo $pieces[4].'</br>';
    echo $pieces[5].'</br>';
    echo $pieces[6].'</br>';
    echo $pieces[7].'</br>';
    echo $pieces[8].'</br>';
    echo $pieces[9].'</br>';
    }

    ?>

    Result
     
    Solved! View solution.
    ironmankho, Nov 18, 2011 IP
  2. avinash gaud

    avinash gaud Member

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    26
    #2
    Hello,

    Try this

    <?php
    $a='Madhuri Dixit,Konkona Sen Sharma,Kunal Kapoor,Divya Dutta,Ranvir Shorey,Vinay Pathak,Jugal Hansraj,Yashpal Sharma,Raghuveer Yadav';
    $pieces = explode(",", $a);


    echo "<pre>";
    print_r($pieces);
    echo "</pre>";
    ?>
     
    avinash gaud, Nov 18, 2011 IP
  3. ironmankho

    ironmankho Active Member

    Messages:
    393
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    Thanks your solution's is good
    but can you tell me what problem in my above code
     
    ironmankho, Nov 18, 2011 IP
  4. #4
    For 9 times (your for loop) you echoed all 9 names. The program was doing exactly what you told it to.

    
    <?php
    $a='Madhuri Dixit,Konkona Sen Sharma,Kunal Kapoor,Divya Dutta,Ranvir  Shorey,Vinay Pathak,Jugal Hansraj,Yashpal Sharma,Raghuveer Yadav';
    $pieces = explode(",", $a);
    
    echo $pieces[0].'</br>';
    echo $pieces[1].'</br>';
    echo $pieces[2].'</br>';
    echo $pieces[3].'</br>';
    echo $pieces[4].'</br>';
    echo $pieces[5].'</br>';
    echo $pieces[6].'</br>';
    echo $pieces[7].'</br>';
    echo $pieces[8].'</br>';
    echo $pieces[9].'</br>';
      
    ?>
    
    PHP:
    would do what you want. So would
    
    <?php
    $a='Madhuri Dixit,Konkona Sen Sharma,Kunal Kapoor,Divya Dutta,Ranvir  Shorey,Vinay Pathak,Jugal Hansraj,Yashpal Sharma,Raghuveer Yadav';
    $pieces = explode(",", $a);
    $letter_count = count($pieces);
    
    $pieces = explode(",", $a);
     for ($i=1; $i<=$letter_count; $i++)
      {
    echo $pieces[$i].'</br>';
      }
    ?>
    PHP:
    (Never put code inside a loop if it can run outside the loop. You were counting how many elements there were in $pieces 9 times. Since the count is the same before the loop or inside it, do that outside the loop. It takes the same amount of time to calculate it each time, so you save 8 execution times that way.)
     
    Rukbat, Nov 18, 2011 IP
  5. ironmankho

    ironmankho Active Member

    Messages:
    393
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    Thanks Rukbat for this .... now i am understand
    little addition : for ($i=0; $i<=$letter_count; $i++) :rolleyes:
     
    Last edited: Nov 18, 2011
    ironmankho, Nov 18, 2011 IP