; on the end of the line

php.internals

dmitr y

9 years ago
Hello, dear coders. I'm using php for a year and it's being my favourite language. Also I'm using some bookkeping languages which is based on php syntax but ';' on the end of the line is not neccesary on it. So I can't understand why syntax needs symbol ';' on the end of every line, if it also ends by 0Ch and 0Dh. Is there a possibility to interpretate php code without ';' on the end of the line, and make it not necessary? Maybe some compilation parameter or directive at code? Best regards, Dmitry

dmitr y

9 years ago
Hello, dear coders. I'm using php for a year and it's being my favourite language. Also I'm using some bookkeping languages which is based on php syntax but ';' on the end of the line is not neccesary on it. So I can't understand why syntax needs symbol ';' on the end of every line, if it also ends by 0Ch and 0Dh. Is there a possibility to interpretate php code without ';' on the end of the line, and make it not necessary? Maybe some compilation parameter or directive at code? Best regards, Dmitry

dmitr y

9 years ago
Hello, dear coders. I'm using php for a year and it's being my favourite language. Also I'm using some bookkeping languages which is based on php syntax but ';' on the end of the line is not neccesary on it. So I can't understand why syntax needs symbol ';' on the end of every line, if it also ends by 0Ch and 0Dh. Is there a possibility to interpretate php code without ';' on the end of the line, and make it not necessary? Maybe some compilation parameter or directive at code? Best regards, Dmitry

Davey

9 years ago
On Wed, Mar 29, 2017 at 11:10 PM, dmitr y <d726627@gmail.com> wrote:
> Hello, dear coders. > I'm using php for a year and it's being my favourite language. Also I'm > using some bookkeping languages which is based on php syntax but ';' on the > end of the line is not neccesary on it. > So I can't understand why syntax needs symbol ';' on the end of every line, > if it also ends by 0Ch and 0Dh. Is there a possibility to interpretate php > code without ';' on the end of the line, and make it not necessary? > > Maybe some compilation parameter or directive at code? > > Best regards, > Dmitry >
Dmitry, It is not necessary to end each line with a semicolon (the ';'), it is however necessary to terminate each statement (or instruction) with one. It's very common to only have one statement per line; which is why is seems that it is required on every line. There are several places this is not the case: - Any block opening/close: e.g. if/while/for/foreach ... { // or it's close, or a comment for that matter! } - An unfinished statement: echo $foo . $bar // concat (.) operator can be here, as above, or on the next line: . $baz; // only semicolon here, to finish the statement - Preceding a closing PHP tag (?>) as this also terminates a statement: echo $foo ?> <?php // or echo $bar ?> Alternatively, you may have MORE than one statement per line: - echo $foo; return $bar; - syslog(LOG_ERR, "Something went wrong!"); exit; - header("Location: /foo/bar"); exit; Notice each of these has two statements. (Note: I don't think any of these are particularly good ways to write readable code, but they are all syntactically valid code). While I know some languages make semicolons optional (javascript), or omit them entirely (python), semicolons in PHP will not be going away (or optional) any time soon. Even a language like Go where they are optional, and largely discouraged, it can be used to explicitly allow the initialization of a local (only exists in the scope of the if statement) variable prior to the condition: if err := DoSomething(); err != nil { // initialization before ; condition { return err // no semicolon necessary } I hope this helps! - Davey

Johannes Schlueter

9 years ago
On Do, 2017-03-30 at 00:07 -0700, Davey Shafik wrote:
> Notice each of these has two statements. (Note: I don't think any of > these > are particularly good ways to write readable code, but they are all > syntactically valid code).
The imo more relevant cases are the one where an optional semicolon causes unclear behavior. Consider we make it optional, what is the behavior of this code: <?php $a = [1, 2, 3] [rand(0, 2)] var_dump($a) ?> $a could either be an array with three elements or a single integer value which is randomly chosen out of the array. Requiring a statement separator like semicolon makes this clear.
> While I know some languages make semicolons optional (javascript), or > omit them entirely (python),  semicolons in PHP will not be going > away (or optional) any time soon.
JavaScript shouldn't be our role model here - it has relatively complex rules for this (essentially "if treating a following line as belonging to the statement leaves valid code then treat it as part of the same statement else terminate the statement on the new line") Python does this by requiring indention on the continuing line. As PHP isn't whitespace-programming this isn't applicable for s either, while it's a way more consistent design than JavaScript in that regard. johannes

Ryan Pallas

9 years ago
On Thu, Mar 30, 2017 at 12:19 PM, Johannes Schlüter <johannes@schlueters.de> wrote:
> On Do, 2017-03-30 at 00:07 -0700, Davey Shafik wrote: > > Notice each of these has two statements. (Note: I don't think any of > > these > > are particularly good ways to write readable code, but they are all > > syntactically valid code). > > The imo more relevant cases are the one where an optional semicolon > causes unclear behavior. Consider we make it optional, what is the > behavior of this code: > > <?php > $a = [1, 2, 3] > [rand(0, 2)] > var_dump($a) > ?> > > $a could either be an array with three elements or a single integer > value which is randomly chosen out of the array. Requiring a statement > separator like semicolon makes this clear. > > > While I know some languages make semicolons optional (javascript), or > > omit them entirely (python), semicolons in PHP will not be going > > away (or optional) any time soon. > > JavaScript shouldn't be our role model here - it has relatively complex > rules for this (essentially "if treating a following line as belonging > to the statement leaves valid code then treat it as part of the same > statement else terminate the statement on the new line") > Python does this by requiring indention on the continuing line. As PHP > isn't whitespace-programming this isn't applicable for s either, while > it's a way more consistent design than JavaScript in that regard. >
I would also point out, that I haven't worked on a JavaScript project in ages where the conventions / rules of that project allow leaving off semi-colons. While the language may make it acceptable, most organizations have rules requiring semi-colons. The reason is to avoid the disambiguation as Johannes has pointed out.