axebox Posted July 16, 2008 Share Posted July 16, 2008 I just came across the need to have a loop's variables be accessible in multiple areas. If there's a loop at the top of your doc, and the same exact one at the bottom - with the same variables, etc.... that's not good. My need originated when I'm coding a php loop with javascript (jquery) output at the top of my doc, and php loop with html output at the bottom of my doc - the problem is they both need to share variables, such as $id, but declaring the same code twice doesn't make sense. What am I doing wrong, or what's good coding practice for this? You never find these kinds of answers in tutorials... Link to comment https://forums.phpfreaks.com/topic/114975-loop-programming-practices/ Share on other sites More sharing options...
Barand Posted July 16, 2008 Share Posted July 16, 2008 Put the code in a function and call the function twice, passing the shared variables function superLooper ($id) { // looping output here using $id } then call the function when required superLooper($id); .. superLooper($id); Link to comment https://forums.phpfreaks.com/topic/114975-loop-programming-practices/#findComment-591326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.