PHP: Hello World!
So now that we have PHP all ready to use, let’s actually get started!“Syntax” is a set of rules that must be followed to write properly structured code (or in most cases – write code that actually works properly). PHP’s syntax is very similar to that of many other programming languages (if you’ve done any programming before). You have different lines of PHP, and each ‘instruction’ ends in a semicolon to tell PHP that we are finished with that bit (you’ll probably understand this more when we begin coding a few lines).
It’s also worth noting that PHP code must be contained in tags of sorts:
<?php //PHP GOES HERE ?>
You put these tags (and obviously all other code you want in your PHP file) into a PHP document (most commonly a .php file) and then either put this onto your remote (or local if you’ve installed PHP on your local machine) server to get it to run. You can then just navigate to your server URL (or if your hosting on your local machine – your loopback address (127.0.0.1 or localhost)) to see how the server serves you the page dynamically from your PHP. If you don’t know how to do this – is should have been in your setup instructions.
So let’s create a new PHP document – I’m going to call mine index.php (for obvious reasons if you’ve done any HTML or anything before, which you really should have done before learning PHP).
Inside the file, we will firstly put a basic HTML structure for our page:
<html> <head> <title>Our Simple PHP Page</title> </head> <body> </body> </html>
This is mainly how we’ll be using PHP. We’ll have a basic HTML structure (like the one above) and then we’ll just put little PHP snippets in using the PHP ‘tags’.
So let’s go ahead and put our PHP tags into the body section of the document:
<body> <?php //PHP GOES HERE ?> </body>
And now it’s all ready for us to put some PHP in!
One of the most basic commands in PHP, is the ‘echo’ command. It most basically outputs a string (a sequence of characters or words) onto the page. We specify the string it outputs by putting it in double quotes (or in fact single quotes if we so wish) after we write the command word ‘echo’.
So let’s put a simple echo command into our document:
<body> <?php echo "Hello World!"; ?> </body>
If you look at the page that get’s served to you when viewing in your browser – you’ll see that the words “Hello World!” get put into the page!
Notice that semicolon after the echo command? That’s what we talked about earlier – we’re telling PHP that we’ve finished the echo command.
It’s worth noting that because it’s simply just putting it into the markup – we are allowed to use HTML in here, for example:
<body> <?php echo "Hello World!
"; ?> </body>
It’s also worth noting that we can do as many of these as we like (and PHP knows that we’re doing a separate command when we type echo again due to that magic semicolon!). For example:
<body> <?php echo "Hello World!
"; echo "This is a test page which was dynamically generated using PHP! :)"; ?> </body>
Another thing that could cause you trouble is if you want to use quotation marks in the string you want to echo. There are however ways around this, the most basic ones being:
- Do a backslash before the double quote in the string to tell PHP that we want to ‘escape’ the quote (and just want it to be in the string)
- Use single quotes to surround the string and double quotes in the string (or use double quotes to surround the string, and single quotes in the string).
One last thing before we end this lesson, note that ‘echo’ is not a function (if you even know what one of those is right now – you may recognise the term from prior programming/scripting). It’s simply a special tool which is built into PHP.
This tutorial was written by Joe Savage
Back to PHP

