lostnucleus Posted May 9, 2010 Share Posted May 9, 2010 Given $data = array('net','cat','wet','bat') then wats the difference between following these two loops ?? <?php foreach($data as $moo)?> <?php { ?> <?php echo $moo;?> <?php } ?> and <?php foreach($data as $moo) { echo $moo; } ?> Thanks in Advance as usual Quote Link to comment Share on other sites More sharing options...
ignace Posted May 9, 2010 Share Posted May 9, 2010 Nothing, except the number of open- and close-tags Quote Link to comment Share on other sites More sharing options...
lostnucleus Posted May 9, 2010 Author Share Posted May 9, 2010 Nothing, except the number of open- and close-tags That wat i though also but they both gives diff output. The first one only outputs the last array element , whereas the second as expected . Quote Link to comment Share on other sites More sharing options...
lostnucleus Posted May 9, 2010 Author Share Posted May 9, 2010 well i found the solution but still looking for an explanation for that <?php foreach($data as $moo) { ?> works good but doing <?php foreach($data as $moo) ?> <?php { ?> dnt work. Quote Link to comment Share on other sites More sharing options...
ignace Posted May 9, 2010 Share Posted May 9, 2010 <?php foreach($data as $moo)?> Is the same as <?php foreach($data as $moo); ?> Therefor $moo contains the last element (as the loops assigns the last element to $moo) and you echo out the last element. This is not a bug I just didn't see it right. Quote Link to comment Share on other sites More sharing options...
lostnucleus Posted May 9, 2010 Author Share Posted May 9, 2010 <?php foreach($data as $moo)?> Is the same as <?php foreach($data as $moo); ?> Therefor $moo contains the last element (as the loops assigns the last element to $moo) and you echo out the last element. This is not a bug I just didn't see it right. nw even i can see it right thx 2 u Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.