Jump to content

Loop programming practices


axebox

Recommended Posts

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

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.