← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 5 of 13

PHP Syntax & Structure

PHP syntax is the grammar of the language. Just as sentences end with a period, each PHP instruction ends with a semicolon so the computer knows where one order stops and the next begins.
Syntax
php
<?php
    statement_one;    // each statement ends with a semicolon
    statement_two;

    if (condition) {
        // block of code in braces
    }
?>

Semicolons

PHP uses a semicolon to mark the end of each statement, the same way a period ends a sentence — it's how the interpreter knows one instruction has finished and the next one is starting.

Omitting it is one of the most common beginner errors, and it usually produces a 'parse error, unexpected token' pointing at the *next* line, not the missing one.

Example: Semicolons

php
<?php
echo "First statement";
echo "Second statement";
// Each statement ends with a semicolon, like a period ends a sentence
?>

Case Sensitivity in PHP

Language keywords and built-in function names in PHP (if, echo, strlen) are case-insensitive, so ECHO and echo behave identically.

Variable names are the opposite: $Name and $name are two completely different variables, so consistent casing matters a lot more for variables than for keywords.

Example: Case Sensitivity in PHP

php
<?php
ECHO "Keywords are case-insensitive.\n";
// Declare `$Name`, set to "Alice"
$Name = "Alice";
// Declare `$name`, set to "Bob"
$name = "Bob";
// Print "$Name and $name are different variables." to the output
echo "$Name and $name are different variables.";
?>

Braces and Blocks

Curly braces { } mark where a block of code begins and ends — the body of a function, the branches of an if/else, or the body of a loop.

Everything inside the braces executes together as a unit, which is what lets you group multiple statements under a single condition or loop.

Example: Braces and Blocks

php
<?php
// Check whether `true`
if (true) {
    // Print "Statement one in the block.\n" to the output
    echo "Statement one in the block.\n";
    // Print "Statement two in the block.\n" to the output
    echo "Statement two in the block.\n";
}
?>

Whitespace and Layout

PHP doesn't care about indentation or extra blank lines the way some languages do — they're purely for human readability.

That said, consistent indentation makes nested blocks (loops inside conditions inside functions) far easier to read at a glance, so most style guides enforce it even though the interpreter doesn't require it.

Example: Whitespace and Layout

php
<?php
    // Print "Indentation is ignored by PHP" to the output
    echo "Indentation is ignored by PHP";


// Print "but blank lines don't matter either." to the output
echo "but blank lines don't matter either.";
?>

Code Nesting

Because PHP tags can appear anywhere inside an HTML file, you can drop small PHP snippets directly into a template — printing a username in a <h1>, say — without converting the whole file to PHP.

The server only executes what's between <?php and ?>; the surrounding HTML passes through untouched.

Example: Code Nesting

php
<?php $username = "Alice"; ?>
<?php // The PHP tag below prints $username directly into the HTML ?>
<h1>Welcome, <?php echo $username; ?></h1>
<p>This HTML passes through untouched.</p>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the semicolon at the end of a statement, so PHP reports a parse error on the following line rather than where the semicolon is missing.
  2. Writing $Name in one place and $name in another, since variable names are case-sensitive and the second is a different, undefined variable.
  3. Leaving a { unclosed or mismatched, causing a 'unexpected end of file' parse error far from the real mistake.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.