Many people start learning a new programming language with a simple program. It is called “Hello world!”. But even this simple program can be written badly in PHP. In this post, I want to show you a good way to write “Hello world!”.

I looked at some websites for beginner programmers. Many of them show “Hello world!” in a bad way. They teach new programmers bad habits. Some examples are also too complicated. They need extra software that is often not necessary. Look at the code below:

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo "<p>Hello World</p>"; ?>
 </body>
</html>

To understand this code, you need to know some HTML. You also need an HTTP server. Many guides tell you to install a LAMP or WAMP stack. But this also installs services you do not need, like a MySQL server.

How can we make this easier for a new programmer? First, do not use LAMP or WAMP at all. Instead of Apache, we can use the HTTP server inside PHP. Just run the code with this command:

php -S 0.0.0.0:80 hello_world.php

This makes it much easier to start with PHP. But we do not need an HTTP server at all. We only want to show the text “Hello world!”. The easiest way is to show it in the console. First, let’s remove all the HTML. This keeps the output clean. Now our code looks like this:

<?php echo "Hello World"; ?>

As you can see, the code is now much shorter. We do not run it with an HTTP server and a browser anymore. You can still do that if you want. Instead, we run it with this command:

php hello_world.php

We can make the code even simpler. We can use the short tag ‘<?=’. This works if you use PHP 5.4 or a newer version.

<?="Hello World"; ?>

There is one more detail about our “Hello world!” example. In PHP, single quotes ‘ and double quotes ” are different. With double quotes, PHP reads and parses the content. That is why the code below works correctly.

<?php
$x = 5;
echo "Hello $x times".

Here is good advice from Bullshit Driven Development: if your string does not use a variable, always use single quotes. This simple rule makes our code faster. We can also remove the closing PHP tag ?>. We do not need it. After all these changes, the final “Hello world!” code looks like this:

<?='Hello World';

But you already know this code. It is the title of this post.

And there is one more way, shorter than everything above. Make a file called hello_world.php and put this inside it:

Hello world!

That is the whole file. No PHP tags, no echo, no semicolon. Open it in the browser and it prints Hello world!.

It works because PHP is a template engine first. Everything outside <?php … ?> goes to the output exactly as written, so a PHP file with no PHP in it is still a valid PHP file. It is also the fastest version on this page, because the engine has nothing to run.

Every other version above is more interesting. This is the one you actually write when you only want to know whether the server works at all.

Follow along
Piotr Hałas — PHP contractor. I write up the production bugs and fixes that actually held: PHP, databases, RabbitMQ, Docker, performance, security. New war story roughly every two weeks — follow along if that's your thing: