break out of an if() block in PHP

By: Strata Ranger Printer Friendly Format    

Although most programmers are aware of this already, if for whatever reason you need to 'break' out of an if() block (which, unlike switch() is not considered a looping structure) just wrap it in an appropriate looping structure, such as a do-while(false):

do if ($foo)
  // Do something first...

  // Shall we continue with this block, or exit now?
  if ($abort_if_block) break;

  // Continue doing something...

} while (false);

Most Viewed Articles (in PHP )

Latest Articles (in PHP)

Comment on this tutorial