Your first PHP script

The opening tag

PHP code lives inside files with the php extension, and the engine only runs what sits between <?php and ?>. Everything outside the tag is sent to the visitor unchanged.

<?php echo "Hello, world!";

php hello.php

Hello, world!

The file hello.php holds the tag and one statement, and the command runs it with the command-line interface. A browser would send the address to a web server instead, and the server would call the same engine and send the same sentence back.

echo prints strings

echo prints text to the output. Give it a string in quotes and end the line with a semicolon:

echo "Hello, world!", PHP_EOL; // Hello, world! echo "Second line", PHP_EOL; // Second line

php two_lines.php

Hello, world!
Second line

echo writes the values it is given and adds nothing of its own, so the constant PHP_EOL is what ends each line here. Without it the two sentences would arrive as one line, Hello, world!Second line.

Run it in the terminal

Save the program from the first section as hello.php and run the file with the php hello.php command shown there.

The terminal prints Hello, world! and nothing else. No server and no browser are needed for plain scripts like this one.

Common beginner mistakes

Three mistakes catch almost every beginner. First, a missing semicolon stops the whole file. Second, code written without the <?php tag never runs: the server sends it back as plain text.

Third, an unclosed quote swallows the rest of the file, so close each string on the same line you open it:

<?php echo "Quotes come in pairs: open and close on one line";

php quotes.php

Quotes come in pairs: open and close on one line

Exercises

  1. Add a second echo to hello.php that prints your name on its own line. Which constant do you need?
  2. Remove the semicolon from hello.php, run php -l hello.php, and read the line that names the error.
  3. Save the same program as hello.html and open it in a browser. What does the visitor see, and why is it not the sentence?
  4. Print three lines with one echo statement and check the output.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which pair of tags marks the PHP code in a file?

    Answer

    <?php and ?>

    The engine runs only what sits between <?php and ?>; the rest of the file is sent to the visitor as it is.

  2. What does this program print? <?php echo "Hello, world!";

    Answer

    Hello, world!

    echo prints the string itself, without the quotes, and the closing tag is optional in a file that holds only PHP.

  3. What does this program print? <?php echo "Hello, world!"; echo "Second line";

    Answer

    Hello, world!Second line

    echo adds no line break, so the two strings print one after the other with nothing between them.

  4. What does this program print? <?php echo "Hello, world!", PHP_EOL; echo "Second line", PHP_EOL;

    Answer

    Hello, world! / Second line

    PHP_EOL is a string holding the line break of the system, and echo prints every argument it is given.

  5. Where does the output of php hello.php go when the command runs in a terminal?

    Answer

    To the terminal, because the CLI prints there

    The command-line interface prints to standard output, which is the terminal the command runs in.

  6. A PHP file holds a line of text before its <?php tag. What does the visitor receive for that line?

    Answer

    The line itself, as plain text

    Everything outside the tag is sent to the visitor unchanged, so a .php file can hold HTML around its PHP.

  7. Which mistake stops the whole file with a parse error?

    Answer

    A missing semicolon at the end of a statement

    The parser needs the semicolon to know where the statement ends; without it the next line is read as part of the same statement.

  8. What does this program print? <?php echo "Quotes come in pairs: open and close on one line";

    Answer

    Quotes come in pairs: open and close on one line

    The quotes are the delimiters of the string, not part of the text that echo prints.

  9. Why does an unclosed quote swallow the rest of the file?

    Answer

    Because the parser keeps reading until it finds the closing quote

    The string ends at the next matching quote, so every line after the open quote is read as part of the string.

  10. Which command checks a file for syntax errors without running it?

    Answer

    php -l hello.php

    php -l lints the file and prints No syntax errors detected in hello.php; -r runs code, -v prints the version and -i prints the configuration.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev