4-2: PHP programming language basics – statements, variables, strings

A formal and comprehensive analysis and explanation of the PHP programming language is beyond the scope of this book. We will instead review the bases of the language through a number of easy to read, understand and implement examples, that will still allow us to write some great and useful code. What follows is intended for complete beginners, it is a “from scratch” guide, so no fears!

In order for you to learn, we recommend that you try all the code we provide yourself. You should be able in most cases to simply copy-paste the provided code into your own pages and get it working immediately. Once it works, you can fiddle with it and make it your own. We encourage you to be proactive instead of just reading along (which is still fine, mind you, but you will not learn as much as if you get your hand a little dirty).

As seen in the previous page, there are some requirements for PHP code to work on your pages or scripts:

  • PHP must be installed on the server
  • Pages with PHP code should have a .php extension
  • The code itself should be surrounded by the following start and end tags:

if on visiting a page you see raw PHP code, then likely you have not implemented correctly some of the above.

Code comments

In PHP, lines that start with a double slash are comments to the code and are totally ignored on execution. It’s a nice idea to use comments to take notes, in plain language, about what we are doing in the code, extremely handy when the code begins to be longer or more complex. If you comment your code, those who will read it will be able to understand it much better. Hint: it may be you in the future, help yourself!

Let’s start by checking that you can indeed execute some PHP code on your server setup.

The “echo” construct

Create an empty page called “test.php” and copy paste the following code into it by using your favourite text editor. If you are working on a Linux machine, we do recommend the shell editor “nano”, that will allow you to work both locally and on a remote server. In alternative you could use gedit or Komodo Edit, for example. Any text editor for code will do.

Script 1: Code to test if PHP is working fine, file name “test.php”

Visit the test.php page with your browser. If everything is working fine you should see a page with:

Hello, world!

in it.

If you do then congratulations, you have your first working php script.

This script is made by a single “statement”. More complex scripts usually include several statements. In PHP, each statement ends in a semicolon. If you omit the semicolon at the end of a statement the script will not work properly.

In PHP, echo is not really a function but rather a language construct, therefore it does not need to be called with parentheses. All it does is to output what follows it (usually a string) to the web page source.

Strings

In the example above, Hello, world! is a string. It is therefore included within quotes:

Double quotes vs single quotes

As in many other programming languages, strings, series of characters, can be delimited by either double quotes, as above, or single quotes:

The main difference between double and single quotes is that, within double quotes, variable names are interpolated to their value, while the content of single quotes is taken literally, as it is. Let’s understand this with an example. For this example we will create a variable, called $a. In PHP, all variable names start with a dollar symbol. We can assign a value, for example a string, to a variable, with the “equal to” sign.

Script 2

In script 2, since the string after echo is within double quotes, the variable $a will be interpolated to it’s value, the string “world”. The result of running this script 2 is exactly the same as running script 1, Hello, world! is shown in the page.

If we use single quotes for delimiting the string that follow the echo, the result is different.

Script 3

$a will not be interpolated to it’s value but rather written in the page source code exactly as it is. We will therefore see:

Hello, $a!

in our web page.

Then why should we want to ever use single quotes for strings, as the variable interpolation thing can be so useful? There are in fact several cases in which using single quotes comes handy. An example is when our string includes some double quotes. Let’s suppose we want to use echo to output a bit of HTML to the source of the page, namely we want to use PHP to write the following to the page:

If we use double quotes to surround the string, then internal double quotes must be “escaped” with a back slash:

Script 4

It can be done, it is often done, but if there are a lot of double quotes within our string this is not very handy and if we forget to escape a double quote the PHP code will end up broken.

Single quotes come to the rescue:

Script 5

Concatenating strings

The dot character “.” can be used to concatenate strings with each other. Here are a few examples.

Script 6

Script 7

Script 8

Creating a full, valid HTML page with PHP

In these examples, that indeed work, the page that is created is not really complete from the HTML point of view, as there is no head section, no body section, no doctype declaration, et-cetera.

Let’s say we want to create a fully valid HTML page with just a red and bold WARNING written on it. We can create an header.html and footer.html pages as described in the previous section, and then use the following code:

Script 9

Or, very much equivalent:

Script 10

Chapter Sections


Work in progress on Chapter 4!

Leave a Reply

Your email address will not be published. Required fields are marked *