Skip to content
Theory · First program · lesson 2 of 7

Statements and comments

In the previous lesson, the program printed lines with the echo command. Back to the recipe card: each step on it ends with a period, so the cook sees where one step ends and the next begins. In the margins the author leaves notes to themselves, and the cook does not carry them out.

A PHP program has both. A semicolon separates the steps, and the notes for people are called comments. Let's see how PHP tells where one command ends.

Statements and the semicolon

Each command of a program is a statement, for example echo "Hello\n";. PHP finds the end of a statement by the semicolon, not by the end of the line.

That is why one statement can take several lines:

PHP
<?php

echo 'One statement ',
    'on three lines',
    "\n"; // One statement on three lines

The text here is in single quotes: 'on three lines' prints the same as "on three lines". Let's see how PHP reads this example:

  1. It starts the statement at the word echo and keeps reading, across the line breaks.
  2. The commas after 'One statement ' and 'on three lines' say that more values are coming.
  3. The semicolon after "\n" finishes the statement, and echo prints everything on one line.

It also works the other way: two statements can share one line, and echo 'a'; echo 'b'; prints ab. PHP understands such a line, but nobody writes code like that: it is harder to read.

Here the recipe picture stops working. A cook understands a step even without a period at the end, while PHP refuses to run the file at all without a semicolon.

echo with several values

echo prints several values in a row when you list them separated by commas:

PHP
<?php

echo 'Hello, ', 'world', "\n"; // Hello, world
echo '2 + 3 = ', 2 + 3, "\n"; // 2 + 3 = 5
echo '2 * 3 = ', 2 * 3, "\n"; // 2 * 3 = 6

Let's take the second line apart value by value:

  1. '2 + 3 = ' is text in quotes, and it prints as it is.
  2. 2 + 3 without quotes is a calculation: PHP adds the numbers and prints 5.
  3. "\n" breaks the line.

The third line works the same way, except that * means multiplication. A + or * inside quotes is part of the text, and without quotes it is an operation on numbers.

Single quotes are used for text that prints as it is. A \n in single quotes does not work: echo 'a\n'; prints the letter a, a backslash and the letter n. That is why a line break always goes in double quotes: "\n".

Comments

A comment is text in a program that PHP skips. Comments are written for people, and they are also used to switch lines of code off for a while.

One-line: // and #

Everything from // or # to the end of the line is a comment, as long as the sign stands outside quotes. The course's examples label their output this way: // 5 at the end of a line means "this line prints 5". Never put [ right after #: since PHP 8, #[ starts another construct of the language rather than a comment, and a file with #[note] will not run.

Multi-line: /* */

Everything between /* and */ is a comment, even when it takes several lines. Let's see which lines run here:

PHP
<?php

echo 'One', "\n"; // One
# echo 'Two', "\n";
/*
echo 'Three', "\n";
echo 'Four', "\n";
*/
echo 'Five', "\n"; // Five

Let's go line by line:

  1. The first echo prints One.
  2. The line with # is a comment as a whole, and Two is not printed.
  3. The lines between /* and */ are a comment too: Three and Four are not printed.
  4. The last echo prints Five.

What PHP ignores

Some of what you write in code goes unnoticed by PHP. That formatting is for the people who read the program.

Spaces and blank lines

PHP skips extra spaces between the parts of a statement and blank lines between statements. Writing echo 'Hi' , "\n" ; works the same as echo 'Hi', "\n";.

Blank lines separate one part of a program from another. The continuation of a long statement is moved 4 spaces to the right, as in the lesson's first example.

The case of keywords

A keyword is a word of the PHP language itself, such as echo. PHP does not care about the case of keywords: ECHO 'Hi'; works the same as echo 'Hi';. The coding style, though, writes them in lowercase.

Common mistake

The comma between the values of echo is missing:

PHP
<?php

echo 'Sum: ' 2 + 3, "\n"; // ✗
TEXT
Parse error: syntax error, unexpected integer "2", expecting "," or ";"

After the text 'Sum: ', PHP expected a comma before the next value or the semicolon that ends the statement. It met the number 2 instead and did not run the file. The fix is to add the comma: echo 'Sum: ', 2 + 3, "\n";.

Key points

  • A statement ends with a semicolon, not with the end of the line.
  • echo prints several values separated by commas, and the line break "\n" goes in double quotes.
  • PHP skips the comments //, # and /* */: they are written for people.
  • PHP ignores extra spaces, blank lines and the case of keywords.