TExcelTAD
Home › PHP › PHP Syntax

PHP Syntax

A PHP script is a plain text file, usually ending in .php. It can contain HTML, PHP code, or a mix of both. This lesson covers the basic rules every PHP file follows.

PHP tags

PHP code lives between an opening tag <?php and an optional closing tag ?>. Anything outside the tags is sent to the output unchanged.

php
<?php
// PHP code goes here
?>
Tag Meaning
<?php Start of PHP code
?> End of PHP code (back to plain output)
<?= Short form of <?php echo — handy in templates

Tip: If a file contains only PHP code, leave out the closing ?>. That way no stray spaces or blank lines after it can accidentally end up in the output.

Mixing text and PHP

Text outside the tags is printed exactly as written, and PHP blocks can appear anywhere in between:

php · try it yourself
<?php $name = "Mia"; ?>
Welcome to the shop!
Signed in as: <?= $name ?>

Items in cart: <?php echo 2 + 1; ?>

Goodbye!
Welcome to the shop!
Signed in as: Mia
Items in cart: 3
Goodbye!

Notice the blank lines in the source after the ?> tags: PHP swallows one newline directly after a closing tag, so an extra line break is needed to keep the next text on its own line. In real HTML templates this rarely matters because browsers ignore most whitespace.

Statements and semicolons

Each PHP statement ends with a semicolon ;. Forgetting one is the most common beginner error and stops the script with a parse error.

php · try it yourself
<?php
$apples = 4;
$pears = 6;
$fruit = $apples + $pears;
echo "Fruit in the basket: ", $fruit, "\n";
Fruit in the basket: 10

Whitespace and line breaks don't matter to PHP, so you could write several statements on one line — but one statement per line is much easier to read.

Case sensitivity

PHP is only partly case-sensitive:

Thing Case-sensitive? Example
Keywords (echo, if, while) No ECHO works like echo
Function names No STRTOUPPER() = strtoupper()
Class names No new datetime = new DateTime
Variable names Yes $color and $Color are different
Constants Yes PHP_EOL must be uppercase
php · try it yourself
<?php
$color = "green";
$Color = "purple";

ECHO "lower: " . $color . "\n";
echo "upper: " . $Color . "\n";
Echo strtoupper("functions ignore case too") . "\n";
lower: green
upper: purple
FUNCTIONS IGNORE CASE TOO

Even though keywords and functions accept any case, the convention is to write them in lowercase. Consistent style helps everyone who reads your code.

Code blocks

Curly braces { } group statements into a block, for example the body of an if statement or a function:

php · try it yourself
<?php
$stock = 0;

if ($stock === 0) {
    echo "Sold out\n";
    echo "Check back next week\n";
}
Sold out
Check back next week

Indent the contents of a block (four spaces is the common standard, called PSR-12) so the structure is visible at a glance.

A note on output

echo is PHP's main way to produce output. The . operator joins strings together, and "\n" adds a newline. You will see both constantly. The constant PHP_EOL ("end of line") is an alternative to "\n" that also works in single-quoted contexts:

php
echo "Line one" . PHP_EOL;

Test yourself

Which of these names refer to the **same** thing in PHP?

  1. $total and $Total
  2. echo and ECHO
  3. $x and x
  4. PHP_EOL and php_eol

Why is it recommended to omit the closing ?> in a file that contains only PHP?

  1. It makes the code run faster
  2. It prevents accidental whitespace being sent to the output
  3. The closing tag is not valid in PHP 8
  4. It enables strict types

What must end most PHP statements?

  1. A period .
  2. A colon :
  3. A semicolon ;
  4. A new line