PHP Syntax

Understand PHP's grammar - tags, statements, comments, case sensitivity, whitespace, and alternative syntax for HTML. The foundation every PHP file is built on.

Beginner 7 min read 8 examples

PHP Tags

PHP code must live between <?php and ?> tags. Everything outside is treated as plain HTML and sent to the browser as-is.

PHP tags.php
<h1>This is plain HTML</h1>
<?php
    echo "This is PHP output";
?>
<p>More plain HTML</p>
<?= "Short echo PHP" ?>

Three tag styles

TagUseRecommended?
<?php ... ?>Standard PHP blockYes (always)
<?= ... ?>Short echo - prints a single expressionYes (always available)
<? ... ?>Short tag - requires short_open_tag=OnNo (removed in PHP 8)
Skip the closing tag in pure PHP files

For files that contain only PHP, omit the closing ?>. This avoids accidental whitespace being sent before HTTP headers, which causes the dreaded "headers already sent" error.

Statements & Semicolons

Every PHP statement ends with a semicolon ;. A statement is one complete instruction.

PHP statements.php
<?php
$name = "Ruban";          // assignment statement
echo $name;               // output statement
echo "Hello";  echo $name;// multiple statements on one line - legal but ugly

// You can split a long statement across multiple lines
$message = "This is a "
         . "long string "
         . "split into lines";

The line does not matter to PHP - the semicolon does. Forget a semicolon and you get a parse error pointing to the next line.

Comments

PHP supports three styles of comments. Use them generously - your future self will thank you.

PHP comments.php
<?php
// Single-line comment (C++ style) - most common

# Single-line comment (shell style) - rarely used

/*
   Multi-line comment
   spans across multiple lines
*/

/**
 * Doc comment (PHPDoc) - used to document
 * functions, classes, and methods.
 *
 * @param string $name The user name
 * @return string Welcome message
 */
function welcome(string $name): string {
    return "Hello, $name";
}
PHPDoc comments matter

The /** ... */ style is read by IDEs (VS Code, PhpStorm) for autocomplete and type hints, and by tools like phpDocumentor for generating API docs. Always document public functions and classes this way.

Case Sensitivity Rules

PHP has unusual case-sensitivity rules. Memorize them now to save future debugging:

ElementCase-sensitive?Example
VariablesYes$name$Name
Constants (defined)YesAPI_KEYapi_key
Function namesNoecho() = ECHO()
Method namesNo$obj->save() = $obj->SAVE()
Class namesNonew User() = new USER()
Keywords (if, echo, while)NoIF = if
Convention always wins

Even though PHP allows ECHO and While, real codebases always use lowercase keywords (echo, while), camelCase methods, PascalCase classes, and UPPER_SNAKE_CASE constants. Match the convention.

Whitespace & Line Breaks

PHP ignores whitespace between tokens. These all work identically:

PHP
<?php
$total = 10 + 5;
$total=10+5;
$total
    =
    10
    +
    5
    ;

Always use the readable version. Most teams follow PSR-12 which mandates 4-space indentation, no tabs.

Alternative Syntax for HTML

When mixing PHP into HTML, curly braces become hard to track. PHP provides an alternative syntax using : and end* keywords:

PHP template.php
<ul>
<?php foreach ($users as $user): ?>
    <li>
        <?= htmlspecialchars($user["name"]) ?>
        <?php if ($user["admin"]): ?>
            <strong>(Admin)</strong>
        <?php endif; ?>
    </li>
<?php endforeach; ?>
</ul>

Each control structure has an end keyword: endif, endfor, endforeach, endwhile, endswitch.

Language Constructs vs Functions

PHP has two kinds of callable things, and they look almost identical:

PHP
<?php
// Language constructs - parentheses are optional
echo "Hello";
echo("Hello");        // works
print "Hello";
include "file.php";

// Real functions - parentheses required
strlen("Hello");
date("Y-m-d");

Language constructs (echo, print, include, require, isset, unset, empty) are part of the language grammar, so they don't need parentheses. Real functions always need them.

Syntax Best Practices

  1. Always use <?php, never <?
  2. Omit the closing ?> in pure PHP files
  3. Use 4 spaces for indentation (PSR-12), never tabs
  4. One statement per line - your diff tool and your eyes will thank you
  5. Lowercase keywords - if, while, true, null
  6. PascalCase classes, camelCase methods, snake_case variables/files
  7. Comment the why, not the what - the code shows what, comments explain why

Next Steps

The syntax foundations are in place. Now move on to working with data:

Frequently Asked Questions

The PHP engine ignores everything outside <?php ... ?> tags and sends it to the browser as-is. Tags tell the engine where PHP code begins and ends. This makes mixing PHP into HTML files easy.

Yes. $name and $Name are two different variables. However, function names, class names, and keywords like echo/IF/While are case-insensitive (though convention is lowercase).

At the end of every statement, yes. The semicolon terminates the statement, not the line. The very last statement before a closing ?> tag can optionally skip the semicolon, but always including it is safer.

Use { } for pure PHP files (cleaner). Use alternative syntax (if: ... endif;) inside HTML templates - it is much easier to read when matching opening and closing tags visually.

Both are single-line comments and behave identically. // is more common in modern PHP code and matches C/Java/JavaScript style. # is occasionally used for shell-script-style configuration files.