September 28, 2008

Escaping the Loop

Get me through this… semi-charmed kind of life. da-da da-da!  I want somethin’ else. To…

Rinse, repeat.  Don’t you hate when you get a stupid song stuck in your head and one line of it just keeps playing over and over and over and over?  It just won’t stop!  So-called earworms often get us frustrated when they crawl in and embed themselves in our unwilling minds.

In the spirit of my continuing problem, I thought I’d look at the functions that PHP provides for escaping their looping structures, namely; break, continue, and return.

break - Completely breaks out of any of the control structures.  You can also specify a number of control structures (levels) to break out of.

continue - Doesn’t completely break out of the control structure just leaves the current loop segment, and starts over at the next evaluation of your condition.  You can also include an option argument to continue out of n number of levels of loops.

return - From within a function, return ends the function immediately, returns the function’s value and passes control back to the line from which it was called.

You are simply brilliant!

Comment by Lindsay — September 29, 2008 @ 3:05 pm

Leave a comment