开发者

Semicolon after closing curly bracket in PHP

I am writing a script to make chunks of a text and send it via SMS. Earlier today i was reviewing some code to split the string in chunks and i saw something I've never seen before. I'm talking about the ";" right after the "}" at the en of the snippet.

Why this colon? i know it works but i don't know if it adds some semantics or some instruction to the interpreter, anybody know what is this for?

while(1)
{
    $length = (strlen($output)+strlen($words[$i]));
    if($length>$new_max)
    {
        if(count($words) > 0)
        {
            $out_array[] = $output;
            $output = 开发者_StackOverflow'';

        }
        else
        {
            break;    
        }
    }
    else
    {
        $output = $output." ".$words[$i];
        ++$i;
    };
};

EDIT: Looks clear that the semicolons as well as multiple semicolons together has no effect over the result, but, do you know if it has some effect to the interpreter? is it doing some task (internally) when it parses it?


I think that those 2 semicolons do nothing here. It is probably being interpreted as an empty expression immediately following the if/while.


These are unnecessary. They don't hurt the script, just unnecessary. The programmer probably had a habit of adding semicolons to end of every statement. Maybe he got this habit from another programming language, maybe his IDE was showing errors without semicolons or maybe he is just a beginner. In any event, there neither hurt nor help. Also the fact that programmer uses while(1) tells me that he is either extremely lazy, because usually a programmer would write while(true), the only reason to type while 1 is because it's less typing to do. A programmer with any type of formal training will not not write while(1) instead of while(true). This is just my opinion, but it's based on over 10 years of programming in 3 languages.


In some cases you need to have a semicolon after a closing curly bracket!

Example:

if(1==1):
    if(true){ echo "it's true"; } //no semicolon here => syntax error!
else:
    echo "no never ever";
endif;

Of course you would't use alternative syntax for control structures if there is only php code.

But if you have a mix of HTML and PHP code you could use the alternative syntax for control structures (alternate syntax makes the code only clearer and easyer to read), but the problem then is that in some cases a plugin or for example a tag parser of a cms could produce an if statement with curly brackets (without a "closing semicolon") just before the "else:" and this would lead to a syntax error in the resulting php file.

The above code would work like this:

if(1==1):
    if(true){ echo "it's true"; }; 
else:
    echo "no never ever";
endif;


Some people use //endwhile or //endif to signal where it came from. Others put semi-colons after the braces. Both techniques are harmless and just comes down to the developer.

I'd hazard a guess that this programmer doesn't trust their own coding, also is coding in a text-book way without style - the brace positions, the inconsistent spacing, and the while(1) is just ... recommend not doing that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜