JSHINER Posted May 8, 2008 Share Posted May 8, 2008 <?php $num_pages = 4; for ($p=1; $p<=$num_pages; $p++) { $pages[] = '<a href="?p=' . $p . '">' . $p . '</a>'; echo join(' | ', $pages); } ?> For some reason the above code is outputting "11 | 21 | 2 | 31 | 2 | 3 | 4" instead of "1 | 2 | 3 | 4" Any ideas why? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 8, 2008 Share Posted May 8, 2008 Try this: <?php $num_pages = 4; for ($p=1; $p<=$num_pages; $p++) { $pages[] = '<a href="?p=' . $p . '">' . $p . '</a>'; } echo impode(' | ', $pages); ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted May 8, 2008 Share Posted May 8, 2008 Because it's in the loop. This should demonstrate the issue: echo join(' | ', $pages), '<br>'; Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 8, 2008 Share Posted May 8, 2008 Or a more graphical representation 11 | 21 | 2 | 31 | 2 | 3 | 4 First pass Second pass Third pass Fourth pass Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 8, 2008 Author Share Posted May 8, 2008 Ah yes it was in the loop. Missed that one. I knew it was something simple Which is better join or implode? Differences? They both have the same result right? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 8, 2008 Share Posted May 8, 2008 my best guess would be that implode is the "newer" way, since there isn't much documentation on join... Correct me if I'm wrong. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 8, 2008 Share Posted May 8, 2008 They're aliases. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted May 8, 2008 Share Posted May 8, 2008 does that mean using "join" is like a behind the scenes redirect to "implode", not sure what they meant by that they were "aliases" Quote Link to comment Share on other sites More sharing options...
JSHINER Posted May 8, 2008 Author Share Posted May 8, 2008 I believe it means they are interchangeable with neither having a more positive / negative effect. Right? Quote Link to comment Share on other sites More sharing options...
effigy Posted May 8, 2008 Share Posted May 8, 2008 Right. 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.