Jump to content

Recommended Posts

For instance, are the last three semicolons required?  Whether are not, is it best practice to or not to include?

$c['errorHandler'] = function($c){
    return function ($request, $response, $exception) use ($c){
        if ($exception instanceof NotFoundException){
            return $c['notFoundHandler']($request, $response);
        } else {
            return $c['response']->withStatus(500)
            ->withHeader('Content-type: text/html')
            ->write('Generic error');
        };
    };
};

Also, from a formatting perspective, what is the best practice where to include them (i.e. on the same line, or the following line)?

            ->write('Generic error');
                ->write('Generic error')
            ;

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.

 

 

The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include or require, so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

 

 

http://php.net/manual/en/language.basic-syntax.instruction-separation.php

 

The semi-colon at the start of the line is programmer preference. When there are many lines it is very easy to spot a missing one. Pick a style and stick to it. I only see it used for SQL statements.

Edited by benanamen

The question is really a no-brainer. Just use the semi in all appropriate places. Don't bother trying to outthink the compiler as to when you can "get away" with not using it. It will only come around to bite you in the rear.

 

Consider this: You get used to leaving the semi off the last line of a brace-enclosed block of code. Then you add a new line to the end of that block and - voila! - you have a compiler error. So now you go back and correct it and wonder why you left such a simple thing off that line just to save a keystroke, only to lose time later on.

 

As Benanamen said above - php wants one at the end of each instruction. Why debate it?

a semi-colon is NOT required used after a }

 

by adding them there, in addition to the wasted typing time and clutter, you are adding an empty php statement to the code, that php must parse when it examines the code. i'm not sure if this actually creates a php byte-code/token, that would then waste time during code execution too.

Edited by mac_gyver
  • Like 1

Do I understand all your positions correctly?

  • Benanamen says it is optional, however, sometimes advantageous not to include.
  • ginerjm says to include them all the time in case the code is later changed.
  • mac_gyver says not to include them.

 

EDIT.  I went searching my code for where I did use them after curly brackets, and came across this.  Not the same context of my original question, but thought I would post this just in case someone in the future views this post.

$arr[$prop]=$this->{$prop};  //semicolon is required
Edited by NotionCommotion

Mac_gyver says not to use them after a brace, which is a true statement. The brace is not a php instruction line. It is simply a wrapper, like a paren.

 

Benanamen says to use and not use depending upon the situation. Wouldn't you really rather just develop a good habit and use them where required and not have to think about it? Basically you put one at the end of every statement except the IF statement. Of course there is probably some syntax/statement that I never use which will probably be pointed out in a few minutes. :)

Agree they should be included where required.  They appear to be only required for very few applications.  My new position is to not included them where they are not required but optional.

<?php

function foo($arr)
{
    if($arr) {
        foreach ($arr->x as $value) {
            $x=$bla->{$value->bla};
        }
        switch($bbb){
            case asd:            
        }
    }
    else {
        while($arr->x) {
            
        }
    }
}

They are required after single statements. Braces surrounding function bodies or conditions etc are block statements and thus do not need a terminating semi-colon.

 

They are required after assigning a closure function because that is a single statement (the assignment).

  • Like 1

Whether or not semicolons are required has nothing whatsoever to do with curly brackets. It depends on the form of the statement. Control flow statements (if, while, ...) have no terminating semicolon. Statements of the form

<expression> ;

(which includes assignments, calls etc.) do require a semicolon.

 

There's no such thing as an optional semicolon in PHP. When you add a semicolon where it doesn't belong, you're creating a new (empty) statement.

Edited by Jacques1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.