PHP Hello World

Write, save, and run your very first PHP program. Learn three different ways to execute PHP - through Apache, the built-in dev server, and the CLI.

Beginner 6 min read 6 examples

Create Your First PHP File

Every PHP file uses the extension .php. Code wrapped in <?php ... ?> tags is executed by PHP - everything outside the tags is sent to the browser as-is.

Create a file called hello.php with this content:

PHP hello.php
<?php
echo "Hello, World!";

That is a complete, working PHP program. Three things to notice:

  • <?php - opening tag that tells the engine "PHP starts here"
  • echo - language construct that outputs a value
  • ; - semicolon ends every PHP statement
Skip the closing tag

When a file contains only PHP, omit the closing ?>. This avoids accidental whitespace being sent to the browser, which would trigger "headers already sent" errors later.

Run via the Browser (Apache)

If you installed XAMPP or Laragon, save hello.php in your web root:

  • XAMPP: C:\xampp\htdocs\hello.php
  • Laragon: C:\laragon\www\hello.php
  • MAMP: /Applications/MAMP/htdocs/hello.php

Start Apache and open http://localhost/hello.php in your browser. You should see:

Hello, World!

Run via PHP Built-in Server

PHP ships with a small development web server. Perfect for quick experiments - no Apache needed. Open a terminal in the folder containing hello.php and run:

Bash
php -S localhost:8000

Now visit http://localhost:8000/hello.php. Press Ctrl+C to stop the server.

Built-in server is for development only

The PHP built-in server handles one request at a time and is not for production use. Use Nginx + PHP-FPM or Apache + mod_php in production.

Run from the Command Line

You can run a PHP file directly from the terminal - no web server, no browser:

Bash
php hello.php
# or explicitly
php -f hello.php

# One-liner without a file
php -r 'echo "Hello from CLI!";'
Hello, World!

This is how cron jobs, deploy scripts, and Composer commands run PHP.

echo vs print

PHP has two output constructs. They are almost identical but differ slightly:

PHP output.php
<?php
// echo - accepts multiple arguments, no return value
echo "Hello", " ", "World", "!\n";

// print - one argument only, returns 1 (usable in expressions)
print "Hello World\n";
$ok = print "Stored";   // $ok = 1

// printf - C-style formatted output
printf("Name: %s, Age: %d\n", "Ruban", 30);
Featureechoprint
Returns valueNoYes (always 1)
Multiple argumentsYes (comma-separated)No
SpeedMarginally fasterMarginally slower
Common useDefault choiceInside expressions only

Mixing PHP with HTML

The real power of PHP is mixing dynamic data into static HTML:

PHP welcome.php
<!DOCTYPE html>
<html>
<head><title>Welcome</title></head>
<body>
    <h1>Welcome, <?php echo "Ruban"; ?>!</h1>
    <p>The server time is: <?php echo date("H:i:s"); ?></p>
    <ul>
    <?php for ($i = 1; $i <= 3; $i++): ?>
        <li>Item <?php echo $i; ?></li>
    <?php endfor; ?>
    </ul>
</body>
</html>

Notice the alternative syntax for: ... endfor; which is friendlier inside HTML than curly braces.

Short Echo <?= ?>

A common shortcut for echoing a single expression is <?= ... ?>:

PHP short-echo.php
<h1>Hello <?= htmlspecialchars($name) ?>!</h1>
<p>Year: <?= date("Y") ?></p>

<!-- equivalent to -->
<h1>Hello <?php echo htmlspecialchars($name); ?>!</h1>
<p>Year: <?php echo date("Y"); ?></p>
Always escape output

Any value coming from a user, database, or external source must be escaped with htmlspecialchars() before echoing into HTML. Otherwise you have an XSS vulnerability.

Common First-Run Errors

Browser shows raw PHP code

You opened the file directly (file:///C:/...). PHP must be processed by a server. Either start Apache and use http://localhost/ or run php -S localhost:8000 in the folder.

Parse error: unexpected end of file

You forgot a semicolon, closing brace, or closing quote. The line number in the error message is where PHP noticed the problem, not necessarily where it started.

Blank white page

A fatal error occurred but display_errors is off. Add this to the top of your script while debugging:

PHP
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);

Next Steps

You wrote and ran your first PHP program. Now learn the language fundamentals:

Frequently Asked Questions

You opened the file directly (file:///...) instead of through a web server. PHP must be processed by a server first. Open the file via http://localhost/... or run php -S localhost:8000 in the file's folder.

echo can output multiple comma-separated arguments and is marginally faster. print behaves like a function and returns 1, which lets you use it inside expressions. In practice everyone uses echo.

For files containing only PHP, omit the closing ?>. This prevents accidental whitespace from being sent to the browser, which would cause "headers already sent" errors. For mixed PHP/HTML files, you still need closing tags.

Avoid <? ?> short tags - they depend on the short_open_tag ini setting which is off by default and removed in PHP 8. Always use the full <?php ... ?> tags. Short echo <?= ?> is always enabled and safe to use.

Add ini_set("display_errors", "1"); error_reporting(E_ALL); at the top of your script, or enable display_errors = On in php.ini. White pages almost always mean a fatal error with display_errors off.