Home>
Question

There is a rule to add a semicolon at the end of a PHP statement.
However, is there a definition of what exactly the "end of PHP statement" means?

Below is the source code written by the author of a text.

Below
・ End of sentence A
There are two semicolons, but is it okay to understand that it is the author's mistake?
(It works fine with two semicolons, but ...)

・ End of sentence B
Foreach's "end of PHP sentence" has a kanji like up to the last}, but there is no semicolon there.
The "end of PHP statement" in foreach looks like "$total + = $n;".

Is there a definition of what exactly the "end of PHP statement" means?

Corresponding source code
<? php
 $total = 0;
 $data = array (98, 76, 54, 56, 78) ;;// A
 foreach ($data as $n) {
    $total + = $n;
} // B
 $ave = $total/count ($data);
?>
php
  • Answer # 1

    There are two semicolons, but is it okay to understand that it is the author's mistake?

    Yes, you don't have to put two in particular.

    (It works fine with two semicolons, but ...)

    Empty sentenceIs interpreted as adding a semicolon to.

    Foreach's "end of PHP sentence" has a kanji like up to the last}, but there is no semicolon there.

    No semicolon is needed at the end of the block.