Why Is the Semicolon Optional in the Last Statement in PHP

Why is the semicolon optional in the last statement in php?

Because the close tag implies a semicolon. You can read more about this in the manual under Instruction separation.

And a quote from there:

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.

An example to prove this:

1. script with missing semicolon at the end, but with closing tag:

<?php
echo "1";
echo "2"
//^ semicolon missing
?>

output:

12

2. script with missing semicolon at the end, but without closing tag:

<?php
echo "1";
echo "2"
//^ semicolon missing (closing tag missing)

output:

Parse error: syntax error, unexpected end of file, expecting ',' or ';' in

Are semicolons in PHP optional?

In general, I would always recommend including the semi-colon, but you're right, it can be dropped in this instance.

You may only drop the semi-colon after a statement when the statement is followed immediately by a closing PHP tag -- ie ?>.

This is documented in the PHP documentation here: http://php.net/manual/en/language.basic-syntax.instruction-separation.php

This is a feature from the earliest days of the language, aimed at making templated code slightly easier to read.

Unlike Javascript, there are no other circumstances where dropping the semi-colon is permitted.

should I put a semi-colon after a single PHP statement?

It might be better to always use a semi-colon :

  • Lowers the probability of errors
    if you need to extend your code in
    the specific section.
  • Improves readability of code

Can I get some simplified explanation about semi-colon use in php?

A semicolon is to denote end of the instruction, and that a new one may start.
When a closing tag is found, no new instructions are expected so it's technically not needed.

It's useful in cases like <?= 'foobar' ?> so you don't need to use a semicolon to echo out the string, but <?= 'foobar'; ?> works just as well.

Do I need a trailing semicolon here?

It is not required, but you should put it, as a good practice.

That way, the day you need to add another instruction after this one, it'll work fine.


And here is the manual's page that answers your question : Instruction separation (quoting, emphasis mine) :

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.

What's the meaning of a semicolon inside a while loop?

This is just a no-op. The loop has an empty body.

It was possibly included to improve readability. A comment could have made it even more obvious:

while (...) {
// do nothing in the loop body
}

You could include any number of semicolons after a statement (which I do not recommend).

All of the following define an empty loop body:

  1. while (...) {
    ;
    }
  2. while (...) {}  // With any whitespace / newlines within the braces
  3. while (...); // Note that the semicolon is REQUIRED in case of missing braces! 
  4. while (...) {
    ;;;;;;
    // any number
    /* and type */
    # of comments
    }

Semicolon after closing curly bracket in PHP

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

What does if(); do, where the semi-colon is right after the parentheses?

A single ; can be read as an "empty statement" and

if($a > 1);
{
....
}

is equivalent to

if($a > 1)
; // execute an empty statement if $a > 1

// then execute the following block of code.
{
....
}


For what I could see the condition seems to be always true when the ; is added

It only seems like it since the block is executed regardless of the if-statement.

Semicolon after if condition in PHP - code still works

I would recommend you to read the manual here:

http://php.net/manual/en/control-structures.if.php

To you question directly why: you don't get a syntax error?

->Simple because there is no syntax error!

Your code is correct and means this:

if ($condition) ;
// ^condition ^if true execute that line

//same as
if ($condition)
;

//same example with other line if the condition is true
if ($condition) echo "true";

if ($condition)
echo "true";

So your line which gets executed if the condition is true is this: ; and means nothing.

it's the same as: ;;;;; It's just nothing!

In most of the time you use a if statement like this:

if ($condition)
echo $result;

if ($condition) {
echo $result;
}

if ($condition) echo $result;


Related Topics



Leave a reply



Submit