PHP supports many useful looping and branching structures, but sometimes a special case calls for a specialized structure
Understanding do{}while(0);
The do{}while(0); structure can be understood as a do{} structure. The while is set to false which will prevent it from looping. PHP doesn’t support a do{} structure, but it can be useful in some situations. The do{} structure provides a block of code that will execute normally, but it can be broken from at any time with a break; statement.
See this very basic, contrived example.
<?php $account_found = false; do{ $account = get_account(); if(!($account instanceof Account)){ break; } if($account->get_status() == 'Closed'){ break; } $account_found = true; }while(0);
This example is short so it would be better served by an if() statement, but when you start to add many conditions to it the if() statement can become very deeply nested. This same thing might also be better accomplished with a function or using exceptions, but this is a very quick method suitable for some one-time use cases. For example, if you need to get multiple variables defined then a function is less efficient because you can only return one variable from a function. If you want execution to continue normally regardless of whether the break statement is called or not, then this approach can produce more elegant code than an exception.
This specialized do{} structure is only good in a few situations, but you will be glad you have it when they come up.