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? Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535936 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>'; Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535939 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 Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535941 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? Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535942 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. Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535943 Share on other sites More sharing options...
effigy Posted May 8, 2008 Share Posted May 8, 2008 They're aliases. Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535947 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" Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535955 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? Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535956 Share on other sites More sharing options...
effigy Posted May 8, 2008 Share Posted May 8, 2008 Right. Link to comment https://forums.phpfreaks.com/topic/104713-solved-strange-problem-should-be-a-simple-solution/#findComment-535979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.