Skip to main content

Hello World (first programs) · Beginner · 14 min

Hello World in PHP: the CLI and the built-in web server

By Padding · Updated 18 Nov 2026
PHP 8.5.12 · php:8.5-cli image (Debian 13) · Ubuntu 26.04 ships PHP 8.5.4

You will run a PHP 8.5 script from the terminal, serve a PHP page with the built-in development server, and greet a visitor by name from the query string with the name safely escaped.

On this page

PHP is the language behind WordPress, and through it a large share of the websites on the internet. It also runs Laravel and Symfony applications, Drupal, MediaWiki (the software behind Wikipedia) and countless small business sites on shared hosting. If you build for the web, you will meet PHP code sooner or later.

Two things make PHP different from most languages you might start with. First, a PHP file is a template: anything outside <?php ... ?> tags is sent out as it is, so you can drop code into the middle of an HTML page. Second, PHP on a web server works per request. Each time a browser asks for a page, PHP starts the script fresh, runs it top to bottom, sends the result and throws everything away. Nothing you store in a variable survives to the next visitor.

In this tutorial you install the PHP command-line interpreter (the CLI), run a script with it, then use PHP's own built-in web server to serve a page and see the per-request model for yourself. You finish by greeting a visitor by name from the URL, escaped so the page cannot be used to inject code. You work in a throwaway folder, ~/hello-php-demo, and delete it at the end.

Step 1 · Install the PHP CLI

On Ubuntu 26.04, install the php-cli package, plus curl to fetch pages from the terminal later:

Bash
sudo apt update
sudo apt install -y php-cli curl

php-cli gives you the php command and nothing else. It does not install Apache or any other web server, which is what you want for learning. Check the version:

Bash
php --version

Ubuntu 26.04 installs PHP 8.5.4, with security fixes backported by Ubuntu. The newest upstream release is PHP 8.5.12. Everything in this tutorial works the same on both.

On Windows, download the latest PHP 8.5 "VS17 x64 Non Thread Safe" zip from windows.php.net/download, unpack it to C:\php, and add C:\php to your Path environment variable (search the Start menu for "Edit the system environment variables"). Open a new terminal and run php --version. WSL with Ubuntu also works and lets you follow the Ubuntu commands exactly. On macOS, install Homebrew from brew.sh, then run brew install php. Homebrew gives you its newest PHP; anything from 8.5 up behaves as described here.

Check it worked

The first line of the output names the version, for example PHP 8.5.4 (cli) ... on Ubuntu or PHP 8.5.12 (cli) ... with the upstream build. The (cli) part tells you this is the command-line interpreter. If you see php: command not found, the install did not finish; run the apt install line again and read its last lines for an error.

Step 2 · Write and run hello.php

Make the demo folder and create the script:

Bash
mkdir -p ~/hello-php-demo
cd ~/hello-php-demo
cat > hello.php <<'PHP'
<?php

echo "Hello, world!\n";
PHP

You can type the same lines into a text editor and save them as hello.php instead. Here is what each line does:

  • <?php switches PHP mode on. Until PHP sees this tag, it treats the file as plain text to copy to the output. It must be the very first thing in the file, with no blank line or space before it.
  • The blank line is only there for readability.
  • echo writes what follows to the output: the terminal here, the browser later.
  • "Hello, world!\n" is a string in double quotes. Inside double quotes, \n becomes a newline character. In single quotes, '\n' would print a backslash and an n.
  • ; ends the statement. PHP needs one after every statement.

There is no closing ?> tag. In a file that holds only PHP code, leaving it off is the recommended style: a stray space or blank line after ?> would be sent as output, which can break pages in ways that are hard to trace.

Run the script, then ask PHP to check its syntax without running it:

Bash
cd ~/hello-php-demo
php hello.php
php -l hello.php

There is no build step. php hello.php reads the file, compiles it to an internal form called opcodes, runs those and exits, all in one go. php -l (lint) only compiles, which makes it a quick way to find typing mistakes.

Check it worked

The first command prints Hello, world! and returns you to the prompt on a new line. The second prints No syntax errors detected in hello.php. If the output shows <?php and the rest of your code as text, the opening tag is misspelt or there is something before it.

Step 3 · Mix PHP into an HTML page

Now write the file a web server will serve. It is mostly HTML, with two pieces of PHP:

Bash
cd ~/hello-php-demo
cat > index.php <<'PHP'
<?php
$visits = 0;
$visits++;
$time = date('H:i:s');
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Hello from PHP</title>
</head>
<body>
  <h1>Hello, world</h1>
  <p>Made at <?= $time ?>. Visit number <?= $visits ?>.</p>
</body>
</html>
PHP

Line by line:

  • $visits = 0; creates a variable. Every PHP variable name starts with $, and you do not declare a type; PHP works it out from the value.
  • $visits++; adds 1, so $visits is now 1.
  • $time = date('H:i:s'); stores the current time as hours, minutes and seconds. PHP uses UTC unless the date.timezone setting says otherwise.
  • ?> switches PHP mode off. Everything after it, down to the next tag, is copied to the output unchanged.
  • <?= $time ?> is the short echo tag. It means <?php echo $time; ?> and is the usual way to print a value inside HTML.

Run it with the CLI first, to see that PHP's job is to produce text:

Bash
cd ~/hello-php-demo
php index.php

Check it worked

The output is a complete HTML page starting with <!doctype html>. The <p> line reads something like <p>Made at 09:41:07. Visit number 1.</p>, with the real time in place of <?= $time ?>. No <?php or <?= appears anywhere in the output.

Step 4 · Serve the page with the built-in web server

PHP includes a small web server for development. Start it from the demo folder, so that folder becomes the site's root:

Bash
cd ~/hello-php-demo
php -S 127.0.0.1:8080
  • -S starts the server.
  • 127.0.0.1 means only this computer can connect. Other machines on your network cannot reach it.
  • 8080 is the port. Ports below 1024 need root, so development servers use numbers like 8080.

The server keeps running in the terminal and prints a line for each request. Leave it running and open a second terminal for the next commands. Request the page twice:

Bash
curl -s http://127.0.0.1:8080/
sleep 1
curl -s http://127.0.0.1:8080/

When you ask for /, the server looks for index.php in the root folder, runs it and sends back what it printed. You can also open http://127.0.0.1:8080/ in a browser on the same computer.

The visit number never goes up. Each request runs index.php from the top in a fresh state, so $visits starts at 0 and ends at 1 every time. To remember anything between requests, a PHP application keeps it outside the script: in a database, a file, or a session.

This server is for development only. It handles one request at a time by default, has no TLS and is not built to face the internet. On a real server you run PHP behind Nginx or Apache with PHP-FPM, as in deploy Laravel with Nginx and PHP-FPM.

Check it worked

Both curl commands print the HTML page, each with a different time and both with Visit number 1.. The terminal running the server shows two log lines ending in [200]: GET /, one per request. If curl prints Connection refused, the server is not running: check the first terminal for an error.

Step 5 · Greet a visitor by name from the query string

Make the page read a name from the URL, such as /?name=Amara. The part after ? is the query string, and PHP puts its values in an array called $_GET.

The tempting version is <?= $_GET['name'] ?>. Never do that. Whatever the visitor types goes straight into your HTML, so a link with ?name=<script>...</script> in it runs that script in the browser of anyone who opens it. This attack is called cross-site scripting (XSS). The fix is to escape the value with htmlspecialchars every time you print something that came from outside.

Leave the server running and replace index.php:

Bash
cd ~/hello-php-demo
cat > index.php <<'PHP'
<?php
$name = $_GET['name'] ?? '';
if (!is_string($name) || trim($name) === '') {
    $name = 'world';
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Hello from PHP</title>
</head>
<body>
  <h1>Hello, <?= htmlspecialchars($name) ?></h1>
</body>
</html>
PHP

The new lines:

  • $_GET['name'] ?? '' reads the name value from the query string. ?? supplies an empty string when there is none, instead of a warning about a missing key.
  • !is_string($name) guards against a URL like ?name[]=x, which makes the value an array rather than text.
  • trim($name) === '' catches a name that is empty or only spaces. In either case the page falls back to world.
  • htmlspecialchars($name) turns <, >, &, " and ' into HTML entities such as &lt;, so the browser shows them as characters instead of treating them as tags. It assumes UTF-8 text, which matches the <meta charset="utf-8"> line.

You do not restart the server. Because PHP reads the file on every request, the next request runs the new code. Try a plain name, a name with tone marks, nothing, and an attack:

Bash
curl -s --get --data-urlencode 'name=Amara' http://127.0.0.1:8080/ | grep '<h1>'
curl -s --get --data-urlencode 'name=Adébáyọ̀' http://127.0.0.1:8080/ | grep '<h1>'
curl -s http://127.0.0.1:8080/ | grep '<h1>'
curl -s --get --data-urlencode 'name=<script>alert(1)</script>' http://127.0.0.1:8080/ | grep '<h1>'

--get --data-urlencode asks curl to build the query string for you and percent-encode anything that is not plain ASCII, the way a browser does. A name like Adébáyọ̀ travels as bytes such as %C3%A9 and comes back out intact, because PHP decodes the query string and htmlspecialchars leaves letters alone. That matters for any site whose users write Yoruba, Hausa, Amharic, Arabic or any other language beyond English.

In a browser, open http://127.0.0.1:8080/?name=Amara and then change the name in the address bar.

Check it worked

The four curl lines print <h1>Hello, Amara</h1>, <h1>Hello, Adébáyọ̀</h1>, <h1>Hello, world</h1> and <h1>Hello, &lt;script&gt;alert(1)&lt;/script&gt;</h1>. In the last one, the tag has become harmless text: a browser shows the words <script>alert(1)</script> after "Hello," and runs nothing.

Step 6 · Recognise the errors beginners hit

Make each mistake on purpose so you know the message when it happens by accident.

A missing semicolon. Write a script with no ; after the first echo and run it:

Bash
cd ~/hello-php-demo
printf '<?php\necho "Hello"\necho "Bye";\n' > broken.php
php broken.php

PHP prints something like PHP Parse error: syntax error, unexpected token "echo", expecting "," or ";" in broken.php on line 3 and runs nothing, not even the first line. It reports line 3 because it only notices the problem when it reaches the next echo. When an error points at a line that looks fine, check the end of the line above.

A misspelt variable. PHP does not stop on an unknown variable; it warns and carries on with an empty value:

Bash
cd ~/hello-php-demo
php -r '$name = "Amara"; echo "Hello, $nmae\n";'

php -r runs the code in quotes without a file. Inside double quotes, $nmae is replaced by the variable's value, and since there is no such variable you get a line like Warning: Undefined variable $nmae in Command line code on line 1 followed by `Hello, ` with no name. On a live site that warning may be hidden, so read the server's log when a value is mysteriously blank.

The port is already in use. With the server from earlier still running, try to start a second one on the same port:

Bash
cd ~/hello-php-demo
php -S 127.0.0.1:8080

PHP replies with something like Failed to listen on 127.0.0.1:8080 (reason: Address already in use) and exits. Either stop the other server or pick another port, such as php -S 127.0.0.1:8081.

Check it worked

You have seen a Parse error naming line 3, an Undefined variable $nmae warning, and the Address already in use message in your own terminal. Each time, the first word of the message (Parse error, Warning, Failed) tells you whether PHP stopped or carried on.

Step 7 · Stop the server and clean up

Go back to the terminal running the server and press Ctrl+C. The prompt returns.

Then delete the demo folder:

Bash
cd ~
rm -rf ~/hello-php-demo

Keep php-cli installed if you plan to carry on with PHP. To remove it, run sudo apt remove php-cli php8.5-cli.

Check it worked

curl http://127.0.0.1:8080/ now fails with a Couldn't connect to server error, and ls ~/hello-php-demo reports No such file or directory.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.