phpcode-separationautomatic-semicolon-insertion

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


I have seen some articles explaining that a semi colon is not required in the last line of a PHP code due to the closing PHP tag (?>). Is this the case or is there any other theory around it?


Solution

  • 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.