Tag Archives: for

The House of C 015: That’s not Cricket! The House of C 15 MAR 2010

You shout “four” when your favourite batsman times a beautiful cover drive which runs all the way down to the boundary rope, you yell “fore” when your favourite golfer lets loose a magnificent drive that travels for absolute miles. But although they sound the same but are spelled completely differently, one should never call them out at the wrong event.

After all, that just wouldn’t be cricket, now would it? ;)

(And for the count, ball boxes are usually a shade of off-white when compared to the rest of the kit in a kit bag. It’s best not to dwell upon it really…)

PHP: Exit a While or For Loop with Break Programming 31 JAN 2010

How do you exit or break out of a running loop before it has finished completing in PHP?

Well luckily PHP makes it pretty easy by providing us with the break control structure, a function that forces the ending of the execution of the current for, foreach, while, do-while, or switch structure.

You can extend break‘s range by utilizing its single numeric argument that tells it how many nested enclosing structures need to in fact be broken out of.

In other words, a bloody useful function to know! :)

Example Usage:

while(true){
echo date('Y/m/d H:i:s') . '<br />';
break;
}

The code above launches an infinite continuing loop that will print out the current time until the application is eventually forced closed. However, by inserting the break command, the date only gets echoed out once before the loop is forcefully terminated.

If this while loop had been contained within another while loop, the code as it stands would simply break out of the first while loop but continue to execute as part of the top while loop – however, if we specify break 2, the application would forcefully exit both while loops and thus finish execution.

Related Link: http://php.net/manual/en/control-structures.break.php