Create a “Hello world!” PHP script

It’s finally time for our hello world script!

First, our PHP web server expects to receive a file named either index.html or index.php. Since .html files cannot parse PHP, we will need to use the PHP extension instead.

Let’s create a new file in our project named index.php

Wherever we wish to write our PHP code, we must use an open PHP tag. This is created with an open bracket, or less than sign, followed by a question mark, and php. There can’t be any spaces between the characters, it has to be all together.

<?php

Now that we’ve created a PHP tag, we can call any language construct or built-in function of PHP. A language construct is a keyword that makes up the base of the PHP language.

The most commonly used language construct is “echo”, which outputs data:

<?php
echo()

Let’s type echo with an open and closed parentheses.

Within the parenthesis we can pass any data to be outputted. This data is called an “expression”. We will pass in a string, which is a set of characters typically surrounded by single quotes.

<?php
echo('Hello world!');

Finally, we can close up this PHP tag with another question mark, then a close bracket, or greater than sign.

<?php
echo('Hello world!')
?>

Let’s refresh the page, and we will see our string outputted to the browser!

Complete and Continue  
Extra lesson content locked
Enroll to access source code, comments & track progress.
Enroll for Free to Unlock