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 Link to comment https://forums.phpfreaks.com/topic/201141-whats-the-difference-between-these-two-loops/ 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 Link to comment https://forums.phpfreaks.com/topic/201141-whats-the-difference-between-these-two-loops/#findComment-1055271 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 . Link to comment https://forums.phpfreaks.com/topic/201141-whats-the-difference-between-these-two-loops/#findComment-1055272 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. Link to comment https://forums.phpfreaks.com/topic/201141-whats-the-difference-between-these-two-loops/#findComment-1055273 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. Link to comment https://forums.phpfreaks.com/topic/201141-whats-the-difference-between-these-two-loops/#findComment-1055283 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 Link to comment https://forums.phpfreaks.com/topic/201141-whats-the-difference-between-these-two-loops/#findComment-1055304 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.