PHP: BREAK and CONTINUE from Nested Routines (Such as loops and switch statements)

One of the most frequent “gotchas” that I run into are breaking out of loops properly.

Its one of those things that I don’t really think about when I’m banging out code,  and that I can miss when I go back to see why things aren’t working.

I’m currently working on a project that requires me to parse through comments entered into our Meditech Hospital Information System,  looking for key phrases and performing certain actions based on these key phrases.

The issue is that the comments are a single string (meaning that users may enter several things into the same field (its a Meditech thing),  and that this is free text.

I won’t bore you with the details, but suffice it to say, “Yay Reg Ex!” 🙂

The routine I’m working on looks to see if the user entered a month if a particular code (EDD) has been entered into the field.

That was easy enough,  but what I found that some users would enter a numeric month day,  or day month.

So I did a preg_match to find that.   I found that the script would hit this every time,  even though I placed it where it should only run if the month string wasn’t found.

I quickly realized that the break statement I placed to break out of the month search loop was only breaking out of the case statement!  So when it went looking for the next month,  it wouldn’t find it and as such would run the digit match routine!

The way to handle this is really easy…both the break and continue statements in PHP have optional integer arguments where you can specify how many structures to break out of!

Don’t worry,  I’m not going to paste in my whole solution and let you find the interesting bits.  You can always contact me if you’d like more detail on the project I describe.

In this example I’ll show you how to use continue from loops and nested loops.
You can use the same logic for breaks.

foreach($results as $row) {

 if ($row['text'] == 'EDD') {
 continue; //this will skip to the next $results;

 foreach($pattern as $check) {
 if ($check['action'] == 'edd') {
 //do something here
 continue; // continue to the next $pattern
 } else {
 if ($something == $thathing) {
 continue 2; // continue to the next $results
 } else {
 break 2; //breaks out of $results


In this example I’ll show you how to break out of a switch case statement

foreach($results as $row) {
 switch($row['text']) {
 case 'edd':
 if ($datacheck == 'yes') { //something indicating that you're finished processing $results
 //some code here
 break 2; //breaks out of both switch and $results
 } else {
 continue; //jumps to next $results
 break; // case break
 //switch value wasn't handled

Hopefully you found this useful. Please feel free to ask questions or suggest improvements in the comments section!


Javascript: Function that returns status for specified Facebook API permission

I’m working on an application where I want to use Facebook user authorization.

Being someone who is wary of over-granting permission to applications there are permissions I only want to ask for if the user is going to do something where they’re needed.

One of these is the friends list.  As the application will allow the user to invite other people as administrators and managers for their particular group,  I only want access to the friends list if this is something they want to do.

I came up with the following function to do the check for a specific permission.

Its important to remember that the user must grant you permission to access their profile,  and are logged in prior to using this function.

First you must ensure that you do initialize your Facebook connection using your application ID.

window.fbAsyncInit = function() {
appId : 'your app id here',
xfbml : true,
version : 'v2.2'

You can get your Facebook App ID here.

In testing, I called it after getting a connected status response from FB.getLoginStatus.

This is the code that calls the function.

This function is synchronous!  That means the script will NOT wait for it to return a value.

….so call it and store the values on initialization, or page load….or before you need it.

FB.getLoginStatus(function(response) {
if (response.status === 'connected') {

FB.api('/me', function(medata) {

//do stuff here


If you’re wondering what the medata object looks like…here it is:

"name":"David Rothbauer",

Now, the function itself:

function checkfbpermission(perm) {

I loop through the child objects permission and status sequentially, the found boolean will tell me when to return the status value

 var found = false;  
 for (var k in data) {
 console.log(JSON.stringify(data[k])); //this is useful for checking that the permission that you're looking for actually exists!
 for (var i=0;i<data[k].length;i++)
 for (var x in data[k][i]) {
 if (found == true) {
  return data[k][i][x];
 if (x == "permission" &&
 data[k][i][x] == perm) {

 //this will tell the script to return the value of the next object (status)
 found = true;
 else {
 found = false;
 return 'no data';

I’m still working out how to best leverage Facebook user integration into my application. I will post more solutions that I think might be helpful as I develop them.

Full Stack Programmer…

One of the frustrating things in my professional life is trying to describe what I do in a neat little title.

Every software related job title in every online database I’ve encountered has aspects that I am proficient in.

Today,  while perusing job listings on LinkedIn I found a term that I don’t recall seeing before:

Full Stack Programmer

I looked for a definition and found this

Yes,  this pretty much describes my skill set.   Previously I would’ve said “end to end”.

Normally I would find being labelled chaffing,  but in this case,  its nice to have a standard way to describe my skill set.