Basic PHP Syntax and Output

Before we can get going, you need to become familiar with some of PHP's basics like where and how do I store php files, how do I edit them, how do I output something on a screen, browser, ...?

Create Your First PHP File

Once you've chosen an editor or ide to work with, we are ready to create our first PHP file. Open your editor and create a new file. This might come as a surprise, but PHP files are just plain and simple text files.
Make sure to always use the extension .php when naming a file. Something like hello_world.php would fit the coming example.

To put the PHP interpreter (the thing that runs your code) to work, the code must be wrapped in so called PHP tags <?php code goes here ?>.

You can execute files from the command line by typing php filename.php.

The Hello World Example

The most simple way to output text in PHP would actually be, to just write the phrase as is into our file and save it.

Hello World

This works, because everything outside of PHP tags is simply ignored by PHP and sent straight to the screen/browser. When working with frontend files for a website, you will often take advantage of this behavior. So basically we're not even cheating here.

Now - a real interpreted (processed by PHP) example would look like this.

<?php echo 'Hello World'; ?>

The echo keyword will output any following string - or data type that can be converted to a string - until the next semicolon.
The semicolon tells PHP that the statement ends here. Alternatively, if all you do is echoing, you could write that like so.

<?= 'Hello World' ?>

<?= is PHP's short echo tag. Notice that we're omitting the semicolon when using the short echo tag. While it's just personal preference to do so, I think it looks cleaner that way.

Mixing PHP and HTML

What you'll often find and do yourself, is mixing small bits of php code into the HTML markup of a web page.

<!doctype html>
<head>
<title><?= 'Hello World' ?></title>
...
<h1><?= 'Hello World' ?></h1>
...

Right now you might be wondering "what the heck, why not just write the Hello World text without any php stuff at all?". And of course, in this case, you are perfectly right. But what PHP is going to allow us here is to print dynamic texts on the screen or to display/hide things based on certain conditions.

<?php if (true): ?>
    <b>You are logged in!</b>
<?php endif ?>

The keyword if tells PHP that we want to check if something is true. The part between the brackets is the thing that is checked. For now we just manually write true here to always show the text. Don't worry about the details for now =)
Here's another example:

What a beautiful <?= date('l') ?>

This would output What a beautiful Sunday on a Sunday. date() is one of PHP's built-in functions and the part between the parentheses is a so-called parameter. When you pass the parameter 'l' to the date function it gives you the current day of the week.
You may have seen functions and parameters in math class at some point f(x), but no worries if you haven't or don't remember.

In programming, functions are like magic bits of code that help you do all kinds of stuff. PHP has well over 1000 built-in functions and you can even define your own functions with very little effort. We'll dig into all of it step by step, so relax if you have a bit of trouble following.

php manual: date() parameters

Storing Data in Variables

Another key element in programming are variables - and we will use them a lot. They are used to store values and allow us to create dynamic websites and applications.

In PHP they start with a $ sign, followed by letters or words and they look like so $name, $age, $month, $price, $username, $email. You are free to choose how you name variables, but always try to name them according to the type of value they hold.

To store data into a variable, we use the assignment operator = which you should have seen in school at some point.

<?php

$name = 'Bob';
$email = '[email protected]';

Look at that ... why is there no ?> at the end? Well, if there's only php code at the end of the file, it's actually a best practice to omit the closing php tag.

PHP also has a number of predefined/reserved variables. While it's unlikely that you would come up with such variable names yourself you should still know about them.

php manual: reserved variables

Ok, since we now have the name and email in variables, every time we want to use them we can refer to both by their variable name instead of writing it out manually all the time.

<?php

$name = 'Bob';
$email = '[email protected]';

echo "Hello my name is $name\nand my email address is $email\n";
Hello my name is Bob
and my email address is [email protected]

Asking yourself what \n is supposed to do? It's a so-called control character and this specific one tells PHP that we want a linebreak. It's like the <br> tag in HTML markup.

If you're running this example in the web browser, take a look at the source code.
In Chrome that would be [right-click] an then select [View page source].

Single- and Multiline Comments

Comments allow us to to add information or documentation right into the code without making them visible to the outside world.
They can also be used to disable parts of the code, when we want to test something.

<?php

// This is a single line comment.
# This is also a single line comment.

/**
 * This is a multiline comment, also known as docBlock.
 * If you want to know more about docBlocks check
 * https://docs.phpdoc.org/guides/docblocks.html 
 */

// You can also temporarily disable parts of your code like so 
// $name = 'Bob';
// $email = '[email protected]';

Alright, so much for the basics. Let's do a quick recap.

  • php code is stored in text files
  • the files should be stored with the extension .php
  • to indicate the start and end of php code we use <?php and ?>
  • everything that is outside of <?php ?> will be printed
  • variables start with a $ sign and we use = to assign a value
  • expressions end with a semicolon