PHP Syntax & echo

PHP · Lesson 3 of 50 7 min read Updated

Every language has grammar rules, and PHP is no different. A missing semicolon or a wrong tag stops your whole page from loading, so it pays to learn the rules properly once.

In this lesson you will learn how PHP code is marked inside a file, how to print output with echo and print, how to write comments, and the small habits that keep modern PHP files clean and safe.

PHP tags

PHP only runs the code between an opening <?php tag and a closing ?> tag. Everything outside the tags is sent to the browser exactly as written. This lets you mix PHP into an HTML page.

PHP
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>School Notice</title>
</head>
<body>
  <h1>Annual Sports Day</h1>
  <?php
  $venue = 'Main Ground';
  $date = '14 December';
  ?>
  <p>Venue: <?= $venue ?></p>
  <p>Date: <?php echo $date; ?></p>
</body>
</html>
Output
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>School Notice</title>
</head>
<body>
  <h1>Annual Sports Day</h1>
    <p>Venue: Main Ground</p>
  <p>Date: 14 December</p>
</body>
</html>

The PHP block that sets $venue and $date prints nothing, so only its leading spaces remain in the output. The tag <?= is a short form of <?php echo, and it is the neatest way to print one value inside HTML.

Statements and semicolons

A PHP program is a list of statements, which are instructions such as "store this value" or "print this text". Each statement ends with a semicolon ;. Line breaks and extra spaces do not matter to PHP, so you can lay code out to make it easy to read.

If you forget a semicolon, PHP stops with a parse error, which means it could not understand the grammar of your file:

PHP
<?php
echo "Hello"
echo "World";
Text
Parse error: syntax error, unexpected token "echo", expecting "," or ";" in hello.php on line 3

Notice that the error points at line 3, but the mistake is at the end of line 2. PHP only notices the problem when it reaches the next word. When an error line looks fine, always check the line above it.

echo and print

echo sends text to the output. It is a language construct, not a function, so the brackets are not needed. You can give echo several values separated by commas.

print does almost the same job, but it takes only one value, and it returns the number 1, so it can be used inside an expression. In practice, most PHP developers use echo.

PHP
<?php
echo "Train to Jaipur leaves at ", "06:10", "\n";
echo "Total seats: ", 72 * 3, "\n";
print "Platform 4\n";

$result = print "print gives back a value\n";
echo $result, "\n";
Output
Train to Jaipur leaves at 06:10
Total seats: 216
Platform 4
print gives back a value
1

The \n inside double quotes means "new line". It works in the terminal. In a browser, line breaks in HTML are ignored, so you use HTML tags such as <p> or <br> instead. You will learn the difference between single and double quotes in Strings.

Comments

Comments are notes for humans. PHP skips them completely. Use them to explain why the code does something, not to repeat what it obviously does.

PHP
<?php
// A single-line comment
# Also a single-line comment, but // is more common

/*
   A multi-line comment.
   Useful for longer notes.
*/

/**
 * A doc comment describes the code below it.
 * Editors such as VS Code show it when you hover.
 */
$gstRate = 18; // a comment can also follow code on the same line

echo "GST rate: $gstRate%\n";
Output
GST rate: 18%

One warning about #: since PHP 8.0, #[ starts an attribute, a special label you will meet in later lessons. A comment that begins #[ is not a comment any more, so use // to be safe.

Case sensitivity

PHP is partly case-sensitive:

  • Variable names are case-sensitive. $name and $Name are two different variables.
  • Keywords and function names are not. echo, ECHO and Echo all work, and so do strtoupper() and STRTOUPPER().

Even though some things work in any case, write keywords and functions in lowercase. Consistent code is easier to read and search.

Files that contain only PHP

Many PHP files contain no HTML at all, such as a file of helper functions or a script you run in the terminal. In these pure PHP files, leave out the closing ?> tag.

The reason is invisible whitespace. Anything after ?>, even a single blank line, is sent as output. If that happens before PHP sends HTTP headers (for example during a redirect), you get the error "headers already sent". Without a closing tag, stray spaces at the end of the file are just part of the PHP code and do no harm.

Strict types

Modern PHP files start with one more line, directly after <?php:

PHP
<?php
declare(strict_types=1);

function addMarks(int $theory, int $practical): int
{
    return $theory + $practical;
}

echo addMarks(62, 25), "\n";
echo addMarks(62, "25"), "\n";

The first call prints 87. The second call stops the script with an error:

Text
Fatal error: Uncaught TypeError: addMarks(): Argument #2 ($practical) must be of type int, string given

Without declare(strict_types=1);, PHP would quietly turn the text "25" into the number 25 and carry on. That sounds helpful, but quiet conversions hide bugs, such as a form field that contains "25 marks". With strict types, PHP tells you straight away that the wrong kind of value arrived.

Rules for declare(strict_types=1);:

  • It must be the very first statement in the file.
  • It applies only to the file it is written in.
  • It affects function calls made from that file.

You will learn about function types in Functions. From now on, every complete example file in this course starts with this line.

Common mistakes

  • Missing semicolon. Read the error line and the line above it.
  • Space or text before <?php. In a pure PHP file, <?php must be the very first thing, with no blank line or space before it.
  • Using $Name when you set $name. PHP gives an "Undefined variable" warning. Check the case.
  • Expecting \n to make a new line in the browser. Use HTML elements for layout in web pages.

Key points

  • PHP code goes between <?php and ?>; text outside the tags is output as it is.
  • <?= $value ?> is the short way to print a value inside HTML.
  • Every statement ends with a semicolon.
  • Use echo for output; print is similar but takes one value and returns 1.
  • Comments use //, # or /* */; doc comments use /** */.
  • Leave out the closing ?> in files that contain only PHP.
  • Start full PHP files with declare(strict_types=1); to catch type mistakes early.

Exercise

Write a pure PHP script called ticket.php that starts with strict types. Store a passenger name and a seat number in two variables, add a comment explaining what the script does, and print one line such as "Passenger Meera has seat 42". Use echo with commas rather than joining the text.

Show solution
PHP
<?php
declare(strict_types=1);

// Prints a simple ticket line for one passenger.
$passenger = 'Meera';
$seat = 42;

echo "Passenger ", $passenger, " has seat ", $seat, "\n";
Output
Passenger Meera has seat 42

The file has no closing ?> tag because it contains only PHP, and declare(strict_types=1); comes straight after <?php.

Quick Quiz

1. What does <?= $city ?> do?

2. Why should a file that contains only PHP leave out the closing ?> tag?

3. Which of these is case-sensitive in PHP?

4. Where must declare(strict_types=1); appear?

Learn PHP with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call