Lesson 7: Learning PHP: programming basics for output, calculation and variables

ByDr.-Ing. Erik Neitzel
Lesson 7: Learning PHP: programming basics for output, calculation and variables

This lesson shows step-by-step how variables are used to calculate and visualize content within dynamic web pages coded in PHP. This is what WordPress does all day. PHP scripts are used to deal with the complexity of integrating your amazing content into your theme’s layout and deliver all that as HTML to your visitor’s browser. In order to achieve that, lots of dynamic control structures take action, which we will talk about in a second. Let’s clarify some basics first.

What is PHP?

PHP is a script language, standing for Hypertext Pre-Processor. This also describes what it actually does: PHP processes data and generates HTML information output based on that data. Data is processed using loops, functions, parameters and other stuff that we will discuss within the next lessons.

PHP can be embedded within HTML pages, or the other way around: HTML output can be included within PHP code. Either way, the web server has to be able to process PHP. If you want PHP to handle database requests, PHP itself has to have certain modules in place, depending on your database system type.

If you ordered a standard hosting package at a standard web hosting provider, the odds are good that your server can handle both PHP and database processing. To be sure please lookup your package details, maybe you have to upgrade your account.

How to use PHP

Let’s get started on using PHP to process some stuff.

Embedding PHP within HTML or in a separate file

No matter which way of embedding you want to use, you can embed PHP using the following approaches:

1
2
3
4
<? ... ?>
<?php ... ?>
<script language="php"> ... </script>
<% ... %>

The usual way, which we will also use within this tutorial, is the second approach.

Generating text and markup output

If you want PHP to show something, use the echo method:

1
2
3
4
5
6
7
<?php
     echo "Hello world!";
     echo "<table>
                  <tr><td>11</td><td>12</td></tr>
                  <tr><td>21</td><td>22</td></tr>
           </table>";
?>

Introduction to PHP variables

The idea of using a script language is that you want something to be pre-processed — let it be a simple math calculation, a text filter or a dynamically generated table based on database data. You will need variables to do all that. Let’s see what variables are all about.

Every PHP variable starts with a dollar sign, and you can name it as you wish:

1
2
3
4
5
6
<?php
     $text = "I am a text.<br />"; // a string
     $a = 5; // an integer
     echo $text;
     echo $a;
?>

Notice that the double slash opens a PHP comment, a string must have quotes in place, an integer does not. Let’s look at what you can do with integer variables.

Calculations with PHP

Calculating with PHP is as simple as it can be:

1
2
3
4
5
6
7
8
9
<?php
     $a = 10;
     $b = 5;
     $c = $a + $b; // $c will be 15
     $d = $a - $b; // $d will be 5
     $e = $a * $b; // $e will be 50
     $f = $a / $b; // $f will be 2
     $g = $a % $b; // % calculates remainders, $g will be 0 since 10 / 5 = 2 with remainder 0
?>

You can use the remainder calculation to do differentiate between odd and even numbers within a table, for example. We will cover dynamically generated tables shortly.

Implementing counters with PHP

Very often you will also need counters, to count how often a loop was executed, for example. Loops are discussed later on. You can implement counters as follows:

1
2
3
4
5
6
7
<?php
 $i = 0; // initial value of our counter
 $i++; // same as $i = $i + 1, so $i will be 1
 ++$i; // same as $i++, but "echo ++$i" results in "0" and $i is incremented afterwards
 $i--; // same as $i = $i - 1, so $i will be -1 (assuming a start value of 0)
 --$i; // same as $i--, but "echo --$i" results in "0" and $i is decremented afterwards
?>

Using Arrays within PHP

An array can be seen as a locker with different boxes. Every position within an array is a box you can put something into:

1
2
3
4
5
6
7
8
<?php
     $locker[0] = "socks";
     $locker[1] = "shirts";
     $locker[2] = "ties";
     $locker[3] = "pants";
     echo "In box number one are $locker[0] <br />";
     echo "In box number two are $locker[1]";
?>

Please note that an array starts with 0 just as your loop counters should.

Take some time to play around with these PHP basics. The next lesson will be about control structures, and you should have a full understanding of both HTML and PHP basics that we’ve covered so far.

The next lessons:

Dr.-Ing. Erik Neitzel