Statements and Semicolons in php

A statement is a collection of PHP code that does something. It can be as simple as a variable assignment or as complicated as a loop with multiple exit points. Here is a small sample of PHP statements, including function calls, assignment, and an if statement

echo “Hello, world”;
myFunction(42, “O’Reilly”);
$a = 1;
$name = “Elphaba”;
$b = $a / 25.0;
if ($a == $b) {
echo “Rhyme? And Reason?”;
}

PHP uses semicolons to separate simple statements. A compound statement that uses curly braces to mark a block of code, such as a conditional test or loop, does not need a semicolon after a closing brace. Unlike in other languages, in PHP the semicolon before the closing brace is not optional:

if ($needed) {
echo “We must have it!”; // semicolon required here
}

The semicolon, however, is optional before a closing PHP tag:

<?php
if ($a == $b) {
 echo "Rhyme? And Reason?";
}
echo "Hello, world" // no semicolon required before closing tag
?>

Leave a Comment