Learn

Short lessons with real code you can edit and run on the page.

Hello World in PHP

Lesson 1 PHP 8.5 Runs in your browser
In short

<?php echo "Hello, World!"; is a complete Hello World script in PHP. With PHP installed, save it as hello.php and run php hello.php in a terminal, the window where you type commands. PHP then prints Hello, World! in that window.

Key facts

  • PHP code runs only after an opening tag such as <?php. Anything before the tag goes to the output unchanged.
  • echo adds no line break after its text, so text from two echo statements runs together on one line.
  • PHP reads a word without quotes as the name of a constant, which is why echo Hello; stops with an Undefined constant error.
  • PHP can report a missing semicolon on the next line of code instead of the line that needs it.
  • It's preferable to end a file that contains only PHP code without the closing ?> tag.

How do you write Hello World in PHP?

Put <?php on the first line and echo "Hello, World!"; on the second. PHP runs the echo line and sends the text between the quotes to the output.

PHP
<?php
echo "Hello, World!";
Output
Hello, World!

Press Run to execute the script, then edit the words inside the quotes and run it again. The page runs your code with its own copy of PHP, called the runner. Like a terminal, the runner shows the output as plain text.

On a website, PHP runs on the server. That's the computer that sends out the site's pages, and visitors receive only what the code outputs, not the code itself.

What does each part of a PHP Hello World script do?

The <?php tag starts PHP, echo outputs text, the quotes hold that text, and the semicolon ends the statement.

  • The <?php tag switches PHP on, and code runs only after it. PHP copies anything before the tag to the output unchanged. Follow the tag with a space or a line break.
  • echo outputs the value that follows it. It's a language construct, a keyword built into PHP. A function is called with parentheses after its name, but echo isn't a function and needs none.
  • The quotes mark where the text starts and ends. PHP calls a piece of quoted text a string.
  • The semicolon ends the statement. A statement is one complete instruction to PHP.

PHP has another keyword for output, print. With a single piece of text, it works like echo, and print "Hello, World!"; outputs Hello, World! as well. The next lesson, echo and print in PHP, compares the two.

Single quotes work too, and this version prints the same text.

PHP
<?php
echo 'Hello, World!';
Output
Hello, World!

Without quotes, a single word stops being text. PHP reads the word Hello in echo Hello; as a constant, which is a name for a value that can't change. No constant called Hello exists, so PHP stops with an Undefined constant error.

Why do two echo statements print on one line?

echo adds no line break or space after its text. PHP outputs exactly the characters you give it, so two statements in a row run together.

PHP
<?php
echo "Hello, World!";
echo "How are you?";
Output
Hello, World!How are you?

End each string with \n, the escape sequence that PHP turns into a line break inside double quotes.

PHP
<?php
echo "Hello, World!\n";
echo "How are you?\n";
Output
Hello, World!
How are you?

Inside single quotes, \n stays a backslash followed by the letter n.

PHP
<?php
echo 'Hello, World!\n';
Output
Hello, World!\n

On a web page, \n doesn't start a new line in ordinary text. Pages are written in HTML, and a browser shows a line break in HTML text as a space. Use a tag such as <br> there instead.

Do you need the closing ?> tag in PHP?

No. A file that contains only PHP code doesn't need the closing ?> tag, which switches PHP off again. It's preferable to leave it out, because a space or a blank line typed after ?> is sent as output.

Stray output can also break a redirect to another page. PHP has to send a redirect before any output, so a stray blank line can stop it with a warning that headers were already sent.

Keep ?> when HTML or plain text comes after the PHP code. PHP outputs everything after the tag as it is, except the one line break directly after ?>.

PHP
<?php echo "Hello, World!"; ?>
This line is plain text.
Output
Hello, World!This line is plain text.

Text on the line after ?> therefore joins the PHP output on the same line. End the string with \n, or leave a blank line after the tag, to start the text on a new line.

Where do semicolons go in PHP?

A semicolon goes at the end of each statement, so echo "Hello, World!"; ends with one. PHP tells statements apart by their semicolons, not by line breaks, which means two statements can share one line.

PHP
<?php echo "Hello, "; echo "World!";
Output
Hello, World!

To put each statement on its own line, paste the code into the PHP formatter.

The closing ?> tag ends a statement too, so the last statement before it can go without a semicolon.

PHP
<?php echo "Hello, World!" ?>
Output
Hello, World!

Without a closing tag, the last statement needs its semicolon. Leave the semicolon off and PHP stops with a parse error, which it reports before running any of the code.

PHP
<?php
echo "Hello, World!"
Output

Parse error: syntax error, unexpected end of file, expecting "," or ";" in /app/main.php on line 2

Error messages from the runner show /app/main.php, because that's where it saves your code. On your own computer, they show the path of your file. A terminal may also print each error twice, once starting with the word PHP, when PHP's log_errors setting sends a copy there.

Why does the error for a missing semicolon name the next line?

PHP notices a missing semicolon only when it reads the code that follows. When the semicolon is missing at the end of line 2, the error names line 3, where PHP found echo instead of the , or ; it expected.

PHP
<?php
echo "Hello, World!"
echo "How are you?";
Output

Parse error: syntax error, unexpected token "echo", expecting "," or ";" in /app/main.php on line 3

When a parse error points at a line that looks fine, check the end of the last line of code before it. The PHP validator reports a missing semicolon between two statements on the line where the semicolon belongs.

How do you run a PHP file?

Run a PHP file from a terminal with php hello.php, or open it through a web server. Both ways need PHP installed on the computer that runs the file, and the Installing PHP lesson shows how to set it up on Windows, macOS and Linux.

Write the file in a code editor or another plain-text editor, not a word processor, because PHP code has to be saved as plain text. Give it a name that ends in .php, not .php.txt, since the .php ending is what tells a web server to hand the file to PHP.

How do you run PHP from the command line?

Open a terminal, go to the folder that holds the file and run php hello.php.

  1. Open a terminal. On Windows, that's PowerShell or Command Prompt, and on macOS, it's the Terminal app.
  2. Go to the folder. Type cd, a space and the folder's path in double quotes, then press Enter. In Command Prompt, type cd /d instead of cd when the folder is on another drive.
  3. Type php hello.php and press Enter.

PHP prints Hello, World! and the terminal waits for your next command. If the prompt for that command appears right after Hello, World! on the same line, end the string with \n.

If the terminal says that php is not recognized or not found, it can't find PHP. Either PHP isn't installed, or PHP's folder isn't on the PATH, which is the list of folders the terminal searches for programs.

For a quick test without a file, php -r runs code given in quotes. Leave out the <?php tag, because it causes a parse error there. This quoting works in both Command Prompt and PowerShell.

Terminal
php -r "echo 'Hello from the command line';"

How do you view a PHP page on localhost?

Run php -S localhost:8000 in the folder that holds the file, then open http://localhost:8000/hello.php in a web browser. The name localhost means your own computer.

Terminal
php -S localhost:8000

That command starts the web server built into PHP, which serves files from the folder you started it in and keeps running until you press Ctrl+C. The server is meant for development and should not be used on a public network.

XAMPP does the same job with the Apache web server, which it installs together with PHP. On Windows, save the file in C:\xampp\htdocs, start Apache in the XAMPP Control Panel and open http://localhost/hello.php.

How do you put PHP inside an HTML page?

Write the page in HTML and open a PHP tag wherever it needs PHP output. PHP sends the HTML around the tags as it is and replaces each PHP block with whatever its code outputs.

PHP
<!DOCTYPE html>
<html>
<head>
<title>My first PHP page</title>
</head>
<body>
<h1><?php echo "Hello, World!"; ?></h1>
</body>
</html>
Output
<!DOCTYPE html>
<html>
<head>
<title>My first PHP page</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>

The output panel shows that HTML as text. Save the code as hello.php and open it on localhost to see the heading.

Should you use short tags in PHP?

No. Use <?php rather than the short <? tag, which works only when the short_open_tag setting is on. PHP comes with two configuration files, php.ini-development and php.ini-production, and both turn it off, but PHP looks for a file named php.ini, not for either of them.

PHP
<? echo "Hello, World!"; ?>
Output
<? echo "Hello, World!"; ?>

Short tags are off in the runner too, so PHP outputs the code as text. On a live site, that sends your code to every visitor. With no php.ini file, the setting is on unless PHP was built with short tags disabled. That's how code with short tags can run on one server and end up in the page source on another.

The short_open_tag setting doesn't affect the short echo tag <?=, so <?= works on every PHP setup. It's a shorter way to write <?php echo, handy inside HTML.

PHP
<p><?= "Hello, World!" ?></p>
Output
<p>Hello, World!</p>

Does Hello World work the same in PHP 8.5 and 8.6?

Yes. A Hello World script prints the same output in PHP 8.5 and 8.6. The runner uses PHP 8.5.12, and PHP 8.6 came out on November 19, 2026.

PHP
<?php
echo PHP_VERSION;
Output
8.5.12

PHP_VERSION is a constant that holds the version of PHP that runs the script. This is the one example in the lesson whose output changes between PHP 8.5 and 8.6. Under PHP 8.6.0, it prints a version that starts with 8.6.0.

Common mistakes with Hello World in PHP

The first mistake below is about how the file gets opened. The other three stop PHP with an error, and each is listed under the first words of its error message.

  • The browser shows the PHP code or a blank page. Both can mean PHP never ran the file. A .php file opened from disk shows its code or a page missing its PHP parts, and an .html file shows none or only part of the PHP. Use a .php file at an address such as http://localhost:8000/hello.php.
  • Parse error: syntax error, unexpected token "echo", expecting "," or ";". A semicolon is missing in front of the echo on the line the message names, usually at the end of the previous line of code. Add it there.
  • Parse error: syntax error, unexpected end of file. PHP reached the end of the file in the middle of a statement. In a script this short, a missing final semicolon or closing double quote is the likely cause.
  • Fatal error: Uncaught Error: Undefined constant "Hello". The quotes are missing, so PHP reads Hello as the name of a constant. Put the text in quotes.

Run this script to see the Undefined constant error, then put quotes around Hello and run it again.

PHP
<?php
echo Hello;
Output

Fatal error: Uncaught Error: Undefined constant "Hello" in /app/main.php:2
Stack trace:
#0 {main}
  thrown in /app/main.php on line 2

Exercise

Fix this script so it runs without an error and prints two lines, Hello, World! and then I am learning PHP. Both lines have to come from echo or print statements in the PHP code.

PHP
<?php
echo "Hello, World!"
echo "I am learning PHP.";
Output
Show the solution
<?php
echo "Hello, World!\n";
echo "I am learning PHP.\n";

Quiz

This quiz has 5 questions. Pick an answer to see why it is right or wrong.

  1. 1What does this script print?

    <?php
    echo "Hello";
    echo "World";
  2. 2What is the preferred way to end a file that contains only PHP code?

  3. 3What does this script print?

    <?php echo "a"; ?>
    b
  4. 4Which of these is true about echo in PHP?

  5. 5Which line number does PHP name in the error for this script?

    <?php
    echo "Hello, World!"
    echo "Bye!";

Frequently asked questions

Do you need a web server to run PHP?
No, PHP runs without a separate web server. The command php hello.php runs a file in a terminal, and php -S localhost:8000 starts the development server built into PHP when you want to view a page on localhost.
Should you put ?> at the end of a PHP file?
It's preferable to leave the closing ?> tag out of a file that contains only PHP code. Without it, no space or blank line after the code can end up in the output by accident. Keep the tag when HTML or plain text follows the PHP code.
Is echo a function in PHP?
No, echo is a language construct, a PHP keyword rather than a function, so it needs no parentheses. In echo('Hi'), the parentheses only wrap the text, as they do around 2 + 3 in (2 + 3) * 4, and are not part of echo.
Is echo faster than print in PHP?
No, echo is not meaningfully faster than print, PHP's other keyword for output. PHP turns a print statement into the same internal instruction as echo. Only echo accepts several values separated by commas, and only print returns a value, which is always 1.
Why does my browser show PHP code instead of the output?
A browser shows PHP code when PHP never ran the file, for example when it was opened from disk. Run php -S localhost:8000 in the file's folder and open the file through that address, such as http://localhost:8000/hello.php, so PHP runs it first.
Is PHP case-sensitive?
PHP is only partly case-sensitive. Keywords such as echo work in any letter case, but variable names are case-sensitive, so $name and $Name are two different variables. Most constant names, such as PHP_VERSION, are case-sensitive too.
How do you check a PHP file for syntax errors?
Run php -l hello.php to check a PHP file for syntax errors without running it. PHP prints 'No syntax errors detected in hello.php' when the file is fine, or the parse error and its line number when it is not.