Isolated blocks
4 % of PHP code has a strange sickness : isolated block. Blocks are a sequence of instructions that are grouped together in the code : blocks are marked with curly braces, and they are usually found after a flow control command, such as if…then, for or switch to group several instruction under the same branching.
Block may be compulsory, like for switch or optional, like for all of them. In fact, blocks don’t need to have an instruction to exist in the code. You may put them about anywhere in the code, around instructions, and they won’t bother anyone.
<?php { phpinfo() ; } ?>
So, how come do we find lone blocks in the code ?
Left over instructions
Most of the time, those are leftover of an old instruction, that was skillfully removed. The comments give it away :
<?php /*if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { // do something } else*/ { // Image not cached or cache outdated, we respond '200 OK' and output the image. //@readfile($filename); //exit($filename); header("Content-type: $mime_type"); header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+6 months")), true); header("Pragma: public"); header("Cache-Control: public"); @readfile($filename); } ?>
You may also find occurrences based on for(), while() or do…while(), though foreach() is never mentioned.
The case of switch
One less obvious situation happens with switch : case and default sometimes gets block, like this :
<?php switch($a) { case 'a': { doSomething() ; } default : { doDefault() ; } } ?>
Case and default don’t need any blocks, but it doesn’t hurt to add them here. After all, case and default are the only situations where they are not used at all. They may also help readability when the case is complex.
This may be included in a coding convention. However, that style of writing is never valid at the project level : not only are those blocks rare, but they are also in a small minority within the project they are coded in. So, this is not a main stream behavior. I don’t see that coming from another langage, though I may be wrong. Or, this may be an IDE artefact, where the case is better rendered if there is a block : that seems far fetched.
Remove lone blocks
As a general rule, isolated blocks are useless and they should trigger a code inspection : remove the lone block, and clean also the code around.