Learn/PHP/1. Hello, output
Lesson 1 of 15
Sign in

PHP, lesson 1

Hello, output

PHP started life as a language for web pages, but it is also a perfectly good language for command-line programs, which is how every exercise in this course runs. A PHP program is a text file, and the PHP interpreter reads it from top to bottom.

The opening tag

Every PHP file begins with <?php. Anything before that tag is treated as plain text and copied straight to the output; everything after it is PHP code. In a file that contains only PHP, you leave out the closing ?> tag, which avoids accidentally printing stray blank lines.

echo

echo writes text to the output. It is a language construct, not a function, so it doesn't need parentheses. Each statement ends with a semicolon; forgetting it is the most common beginner error, and PHP will report a parse error pointing at the next line.

echo does not add a new line for you. Put \n (a newline character) at the end of the text, or use the constant PHP_EOL:

2 exercises to try what you just read.

Exercise 1

Your first program

Press Ctrl Enter or Check to test your code.

Exercise 2

Escape artist

Press Ctrl Enter or Check to test your code.

There is also print, which works almost the same way but takes exactly one value. Most PHP code uses echo.

Single and double quotes

PHP has two kinds of string literals, and they behave differently:

  • Double quotes understand escape sequences such as \n (newline), \t (tab), \" (a double quote) and \\ (a backslash). They also insert variables, which you'll meet in the next lesson.
  • Single quotes are taken almost literally. Only \' and \\ are special, so '\n' is a backslash followed by the letter n, not a newline.

Use single quotes when the text is fixed, double quotes when you need escapes or variables.

Joining text

The dot operator . joins (concatenates) two strings. Note that + does not join text in PHP; it only adds numbers.

Comments

Comments are notes for people. PHP ignores them:

How a script runs

PHP first parses the whole file. If there is a syntax error anywhere, nothing runs at all. If the file parses, PHP executes the statements in order. When something goes wrong while running, PHP prints an error message with the file and line number. Read it carefully: it usually points right at the problem, or the line just after it.

Further reading

  • Basic syntax: PHP tags
  • echo
  • Strings
  • Comments

Write a program that prints exactly these two lines:

The program reads no input.

Print these three lines exactly. They contain double quotes, a backslash and a tab (the gap in the last line is a single tab character, \t).

<?php

echo "Hello, World!\n";
<?php

echo "First line\n";
echo "Second line", PHP_EOL;   // echo accepts several values separated by commas
echo "Score: ", 42, "\n";      // numbers are converted to text automatically
<?php

echo "Tab\tseparated\n";
echo 'No newline here: \n', "\n";
echo "She said \"hi\"\n";
echo 'It\'s fine', "\n";
<?php

echo "Hello" . ", " . "PHP" . "\n";
<?php

// a one-line comment
# also a one-line comment
/* a comment that can
   span several lines */
echo "Comments don't print\n";
Hello, World!
I am learning PHP.
The robot said "beep boop".
Files live in C:\php\scripts
Name:	Robo