# ; on the end of the line

- Thread: https://externals.io/message/98673
- Original archive: https://news-web.php.net/php.internals/98673
- Started by dmitr y on 2017-03-30T06:10:52+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #98673 — dmitr y — 2017-03-30T06:10:52+00:00

URL: https://externals.io/message/98673#98673

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

---

## #98671 — dmitr y — 2017-03-30T06:14:07+00:00

URL: https://externals.io/message/98673#98671 · In reply to: #98673

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

---

## #98672 — dmitr y — 2017-03-30T06:14:25+00:00

URL: https://externals.io/message/98673#98672 · In reply to: #98671

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

---

## #98674 — Davey Shafik — 2017-03-30T07:07:18+00:00

URL: https://externals.io/message/98673#98674 · In reply to: #98673

> 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

---

## #98691 — johannes@schlueters.de — 2017-03-30T18:19:09+00:00

URL: https://externals.io/message/98673#98691 · In reply to: #98674

> 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

---

## #98692 — Ryan Pallas — 2017-03-30T18:25:51+00:00

URL: https://externals.io/message/98673#98692 · In reply to: #98691

On Thu, Mar 30, 2017 at 12:19 PM, Johannes Schlüter <johannes@schlueters.de>
wrote:

> 
> > […]
>
> 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.
>
> > […]
>
> 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.


>
> johannes
