After having read about the PHP basics, we’re now providing a slight overview of different PHP fonctionalities:

A PHP instruction always starts with <?php and ends with ?>. It can be written directly in your HMTL code. To differentiate, a index.html file would now be saved as index.php in order to have the code correctly interpreted by the PHP interpreter.

PHP variables

Let’s start with variables. A variable is a data that may take different values over time. What kind of values you may ask? Let’s take an example: You are creating a website and you would like to create a specific customer area. As you will hopefully have more than one customer, you will need to set up once (!) a specific customer registration form that allows all customers to register. One of the fields in this form might be the name of the customer. Defining this one as a variable in your form allows you to generate a form for all customers, each having his/her own name. 🙂 In order to define the name as variable in the form you have to put a $ in front of name, thus $name.

A variable can not only be a character string like name, but also a number for instance. In total there exist four different types of variables:

  • string (character string)
  • integer (whole number)
  • bool (boolean which means binary logic – true or false)
  • float (real number)

How does this look like in PHP? Please see the example below:

<?php
$user='superuser';
$age=40;
$houseowner=true;
$housesizesqm=120.5;
?>

As you can see, all instructions are within the tag <?php ?> and each one ends with a “;”. In this example we now just defined the variables $user, $age, $houseowner and $housesizesqm. How can these ones be shown? This brings us to introduce echo.

PHP instruction: echo

echo is the command line to show content. Let’s check our previous example:

<?php
echo'<p>Hello '.$user.'</p>';
?>

The echo line says Hello to the registered user by indicating its registered user name after the word “Hello”, thus “Hello superuser” in our case. This can be useful when you want to customize your website for each one of your clients in order to create loyalty and customer retention for instance.

PHP if statement

We could now extend this example by adding an if statement, just like stated below:

<?php
echo'<p>Hello '.$user.'<br/>
if ($houseowner==true){
echo 'You are a house owner.</p>';
}
else{
echo 'You don't own a house.</p>';
}
?>

After the echo command showing the greeting, <br/> indicates to go to the next line and deal with the if statement. To keep it simple, the command tells you that if the user is a house owner, “You are a house owner” will be displayed, otherwise “You don’t own a house” will be shown. As you can see in order to compare a value, one uses “==” in PHP. This is one of the assignment operators of PHP. To keep it short, there are many others, such as “!=” for unequal or “<=” for smaller or equal to.

The if statements allow you to show specific information to a specific target audience such as country specific promotions on your website for instance.

<?php
echo'<p>Hello '.$user.'<br/>
if ($country==Brazil){
echo 'Just for you: Today's special promotion</p>';
}
else{
echo 'Nice to see you back here</p>';
}
?>

Let’s just quickly get back to the echo as this can be very helpful in case of debugging your website for instance. Here an example:

<?php
echo '<pre>';
print_r($user);
echo '</pre>';
die;
?>

You put the above echo at the beginning of your page where there is an error. If the variable $user appears, then this means your problem is further down on the page. Thus you put your echo further down until the $user doesn’t appear anymore. This means your problem is just before the echo and you can now fix this one.

PHP Forms

In order to get all the information from the user as in the previous example with $user, $age, $houseowner and $housesizesqm, we could have asked this information by letting the user fill out a form on the website, setting up the following PHP code (to simplify, we just ask for user name here):

<form name="registration" method="post" action="login.php">
Please enter your user name: <input type="text" name="user"/> <br/>
<input type="submit" name="submit" value="OK"/>
</form>

This one will now be implemented in the document login.php in order to collect the data from all users. Here’s what this may look like:

<html>
<head><title>Login page</title></head>
<body>
<h1>Welcome to our community</h1>
<h2>Please sign up</h2>
<form name="registration" method="post" action="login.php">
Please enter your user name: <input type="text" name="user"/> <br/>
<input type="submit" name="submit" value="OK"/>
</form>
</body>
</html>

With the if statement seen above, we could then display the entered data.

As mentioned above, this can be very useful when you want to create a customer area on your website.

PHP instruction: function

Let’s continue with another PHP instruction: function. Assuming, you want to highlight your different target markets by using a color code. All leads coming from France should be highlighted in blue, those ones coming from the USA in red and the rest of the World in the standard setting black. One would of course need to define the variable $country first, as seen above. We’ll skip this here and go directly to the function instruction. Here is what we’re looking for:

<?php
function colorcode($country){
if($country==France){
echo'<font color="blue">'.$country.'</font>';
}
elseif($country==USA){
echo'<font color="red">'.$country.'</font>';
}
else{
echo $country;
}
}
?>

What does the above code mean? If the country is France then show the result in blue and you’re done. If this is not the case, then check if the country is USA. If the country is USA, then show the result in red and you’re done. If this is not the case, then show the result in the standard setting black and you’re done.

PHP tables

Imagine, you may want to create a country table including different variables such as $France, $USA, etc. This could be done the following way:

<?php
$countries=array('France','USA','China','India','Canada','Russia','Brazil');
?>

You can now display them with the echo instruction by indicating the position of the variable within the table. This position is called “index” and it starts at “0”. This means if you want to display the variable $China, then the echo is

<?php
echo $countries[2];
?>

Of course there exist many other PHP instructions. However, we’ll end this article here, as it should just give you a slight overview of what’s possible with PHP.