Skip to content

Exercise 1 of 10 · Output

First Lines of PHP

What you will make

Two lines of welcome text printed to the screen, from code you repaired yourself.

The one new idea: echo prints text, and every statement ends with a semicolon

echo is how a PHP script shows you anything at all. Every exercise after this one ends with something printed, and reading that output is how you will check whether your code actually did what you meant.

Go straight to the code ↓

What echo and ; do

PHP doesn't show you anything unless you tell it to. echo is the command that puts text on the screen, and it prints exactly what you give it — nothing more, nothing less, and it never adds a line break on its own. If you want output to land on its own line, you put \n inside the string yourself.

Every instruction in PHP also has to end with a semicolon (;), the same way an English sentence ends with a period. The semicolon is how PHP's parser knows where one instruction finishes and the next one begins. Leave it off, and PHP doesn't stop at the mistake — it keeps reading, trying to make sense of two instructions as if they were one, until it gives up at whatever comes next.

A worked example

Here's a different short program, so you can see the pattern without being handed today's answer:

PHP
<?php
echo "Step 1: mix\n";
echo "Step 2: bake\n";
echo "Step 3: done!\n";

Running it prints:

Output
Step 1: mix
Step 2: bake
Step 3: done!

Three echo lines, three lines of output, in the exact order they were written. Each string ends with \n, so each one lands on its own line. Remove that \n from the first line and the second echo's text would print right after it, on the same line, with nothing to separate them.

Your turn

The starter program is supposed to print two lines: a welcome message, then a second line saying you're getting started. Right now it doesn't run at all, because one of the two echo statements is missing something the other one has.

Compare the two lines carefully, character by character if you need to. Add the missing semicolon to the end of the first line, right after the closing quote and before the newline, then press Run. That's the whole task — you're finishing a line that's already almost complete, not writing anything new.

If something goes wrong

If PHP reports a syntax error, look at the line it names, and then look at the line directly above it too — a missing semicolon is only noticed once PHP reaches the next statement, so the error message often points at the wrong line. If your output runs two lines together with no break between them, look for a missing \n inside one of the strings instead. And if nothing prints at all, double-check that the very first line of the file is still <?php — without it, PHP treats the whole file as plain text instead of code.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Leaving a semicolon off the end of a statement
PHP keeps reading past the mistake until it hits the next statement, so the syntax error names the wrong line. When an error doesn't make sense where it points, check the line just above it.
Deleting \n because it looks like decoration
echo never starts a new line on its own. Without \n inside the string, the next echo's text is printed immediately after it, on the same line.
Putting a semicolon after <?php
<?php opens a block of PHP code; it isn't a statement itself and never takes a semicolon.

Want a blank editor instead? Open the PHP playground.