PHP Hello World Program

The classic first PHP program: print "Hello, World!". Edit the code below and press Run to execute it with real PHP in your browser.

Code Editor
Output
Hello, World!

How this hello world example works

Every PHP script starts with the opening tag <?php. echo sends text to the output, and the statement ends with a semicolon. The closing ?> tag is optional and is usually left out in files that contain only PHP, which avoids accidental whitespace being sent after it.

echo vs print

Both output text. echo accepts several comma-separated arguments and returns nothing; print takes one argument and returns 1, so it can be used in expressions. In practice echo is used almost everywhere.

<?php
echo "Hello", ", ", "World!", "\n";
print "Hello from print\n";
$name = "PHP";
echo "Hello, $name!\n";       // variables are interpolated in double quotes
echo 'Hello, $name!' . "\n";  // but not in single quotes

Output:

Hello, World!
Hello from print
Hello, PHP!
Hello, $name!

Hello World inside an HTML page

Saved as index.php on a web server, PHP runs the code between the tags and sends the rest of the file as HTML. <?= ... ?> is short for <?php echo ... ?>.

<!DOCTYPE html>
<html>
<body>
    <h1><?= "Hello, World!" ?></h1>
    <p>Today is <?php echo date("l"); ?>.</p>
</body>
</html>

Run hello.php on your computer

With PHP installed, save the program as hello.php and run php hello.php in a terminal, or start the built-in server with php -S localhost:8000 and open http://localhost:8000/hello.php. No installation? Press Run above: the code executes with real PHP compiled to WebAssembly in your browser.

Hello World FAQ

How do I write Hello World in PHP?

Create a file containing <?php echo "Hello, World!"; and run it with php hello.php, open it through a web server, or paste it into the editor on this page and press Run.

Do I need the closing ?> tag?

No. In files that contain only PHP code it is recommended to omit it.

Why is the newline not showing in my browser?

Browsers ignore "\n" in HTML. Use <br>, wrap the output in <pre>, or use nl2br().