Practicing with ForEach


If i replace the $word with & sentence in the foreach & echo statement I still get the same result. Why ? What is the logic ? Please help

 $sentence = array("I'm ", "learning ", "PHP!");
        foreach($sentence as $word){
          echo $word;


I assume you mean when you replace $word with $sentence you mean this:

$sentence = array("I'm ", "learning ", "PHP!");
foreach($sentence as $sentence){
    echo $sentence;

The reason you get the same result is that each time the loop runs through the array $sentence, it takes the variable $sentence[i] and assigns it using the identifier $sentence.

Within the foreach loop, $sentence thus refers to the individual array element, not the array itself.


Thanks for the explanation. That helped !


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.