Learnit Training

3. Programming

3.1. Variables

3.1.1. What is variable?

A computer's memory is sometimes compared to a chest of drawers, with drawers that can have names and contain all kinds of content. This is a good way to think about variables. A variable is such a drawer, with a name and a content. Different drawers cannot have the same name, that would cause confusion. However, it is no problem if they have the same content. For example, the name of a drawer could be "place" and its contents "Amsterdam". There is then a variable called "place" that has the value "Amsterdam". In PHP we write it that way:

$place = "Amsterdam";

Remark: The $ sign always precedes the name of a variable. The = character is used to assign a value; that to the right of the = character is assigned to the variable to the left of the character. The " characters around the value are used to indicate that what is in between is a so-called string (more on this later). Furthermore, in PHP every line is closed with ;.

We can always create a new drawer. We can change the contents of drawers. We can copy them. The contents of a drawer can be added to the contents of another drawer. If the contents are a number, we can even calculate with them!

  • New variable:
    $place = "Amsterdam";
    $firstname = "Piet";
    $last name = "Jansen";
    
    There are now three variables with three different values.
  • Change value:
    $place = "Amsterdam";
    $place = "Rotterdam";
    
    First, the variable $place the value "Amsterdam"on the second line it shall be changed to "Rotterdam".
  • Copy value:
    $place = "Amsterdam";
    $residence = $place;
    
    The value of the variable $place is assigned to the variable on the second line $residence.
  • Merge values:
    $firstname = "Piet";
    $last name = "Jansen";
    $name = $firstname . " " . $lastname ;
    
    The variable $name is given the value "Piet Jansen".
  • Numbers:
    $number1 = 20;
    $number2 = 5;
    $result = $number1 + $number2;
    
    The variable $outcome is given the value 25.

    Tip: In almost all programming languages, and also in PHP, addition is done with +Subtract with -multiply by *, share with / and power equalisation with ^.

Remark: We have already seen in the previous chapter that with the command echo whatever is behind it is sent to the output. In our case, this always means that everything behind echo state in the web browser.

Remark: PHP files always start with a line where <?php state. They always end with a line where ?> state. All other lines always end with ;.

Assignment 3.1.

  1. Create a file op-3-1.php in the phpcourse/ folder and open it in Notepad, then run the following commands on a new line each time.
  2. Make sure the file starts and ends correctly.
  3. Create a new variable called name with the value klaas (in the previously mentioned string notation)
  4. Send the value of name to the web browser.
  5. Change the value of name to henk.
  6. Again, send the value of name to the web browser.
  7. See the result on http://localhost/phpcursus/op-3-1.php
  8. If all goes well, the result will be a window that prints out klaashenk (without space or enter)
  9. Think of a way to make the string "
    "
    (the XHTML code for a new line) between klaas and henk to get.
  10. Check the result again to see if it was successful.
  11. Compare the content of the file you have created with the content of op-3-1.txt

    Note: The file you are comparing with has the extension .txt, this is to prevent the content from being interpreted as PHP and you therefore no longer being able to see what the code was.

3.1.2. Other types of variables

So far, without really knowing it, we have already used several types of variables, namely strings and integers. Below is a complete list of the variable types available in PHP, with examples of the values the variables can hold.

  • Integrity: Entire numbers
    $a = 3;
    $b = -16;
    $c = 130;
    
  • Double: Double precision
    $a = 0.9;
    $b = -19.734;
    $c = 100.001;
    
  • Float: Floating comma
    $a = 4e-15;
    $b = 6.000000000001;
    $c = 15.1;
    
  • Boolean: Boolean
    $a = True;
    $b = False;
    
  • Char: Characters
    $a = 'b';
    $b = '6';
    $c = '#';
    
  • String: Character set
    $a = "I am a string";
    $b = "String";
    $c = "";
    
  • Array: Variable that can contain multiple variables
    $a = array("a", "b", 12, 5.0, True)
    $b = array("a", "b", "c");
    $c[0] = "a";
    $c[1] = "b";
    $c[2] = "c";
    
    Remark: Note that $b and $c are the same.

3.2. Conditions

In almost every PHP script, it will be necessary to check whether variables meet certain conditions: has a certain date already occurred, is the entered password correct, is one number greater than another?

The structure of such conditions looks like:

  • If a condition is true
    • Perform this if the condition is true
  • Otherwise
    • Perform this if the condition is false

In PHP we can create the following script that uses this structure:

 $leeftijd_piet) {
    echo "Jan is ouder dan Piet";
    }
else {
    echo "Jan is jonger dan Piet";
    }
?>

Remark: Note the variable names above a horizontal dash, _It is customary to do so when the name of a variable consists of two parts. Furthermore, the greater than sign, >, used to compare the values of 2 variables.

Assignment 3.2.

  1. Create a file op-3-2.php in the phpcourse/ folder and open it in Notepad.
  2. Paste the code from the above example into the file.
  3. Think about what the outcome of the script should be.
  4. Check this by going to http://localhost/phpcursus/op-3-2.php.
  5. Adjust the ages of Pete and John so that Pete is younger than John and look at the result again.

3.2.1. Comparison operators

In the above example, we used the greater than sign, >to compare two variables. The > sign is a comparison operator. The idea of such an operator is that the variables on two sides are compared. The result of such a comparison is always true or false, True or False. Other operators are:

Table 3.1. Comparison operators
operatorstands for
>greater than
<less than
>=greater than or equal to
<=less than or equal to
==equal to
!=unequal to

Assignment 3.3.

  1. Create a file op-3-3.php in the phpcourse/ folder and open it in Notepad.
  2. Paste the following script into it:
  3. Save the file, but do not close it.
  4. See http://localhost/phpcursus/op-3-3.php for the result.
  5. The result is False (1 is always not equal to 2).
  6. This result has already been entered in the table below. Complete this table (on paper), by:
    • the values of $number1 and $number2 change.
    • change the comparison operator.
    • save the changes.
    • always refresh the web browser with the result (F5).
  7. Table 3.2. Exercising with comparison operators
    $number1operator$number2result
    1==2False
    3==3...
    1<=1...
    5>6...
    5!=6...
    3!=3...

3.3. Loops

So far, programming has only cost us more time. We could have just put Untrue into a file, instead of the much more complicated codes that give the same result. And even though what we have learned so far will prove to be important, using loops is the ultimate advantage of programming!

A loop is used to repeat certain pieces of code a certain number of times. There are three types of loops, but in this course we will only discuss the for loop. The other two, while-loop and do..while-loop are just variations of it.

3.3.1. For-loop

Suppose we want to have the numbers 1 to 20 listed on the screen, we can just type them in. Twenty numbers is fine, but as soon as more are typed, it takes a lot of time. We can also write a script with a loop that does this for us. Such a script would look like this:

\n";
    }
?>

The operation of this script can be illustrated with a flowchart:

Or in words:

  1. The variable $i on 1 set (initialised).
  2. It shall be verified that the value of $i (1 in this case), is smaller than 20.
  3. If this is the case, the line $i . "
    ("1
    "
    in this case) to the browser.
  4. The variable $i with 1 raised (by $i++), and therefore becomes 2.
  5. It shall be verified that the value of $i is smaller than 20.
  6. If this is the case, the line $i . "
    sent to the browser.
  7. The variable $i with 1 elevated.
  8. Steps 5 to 7 are repeated until $i is equal to 20.

The3rd line is simply executed 20 times.

Task 3.4.

  1. Create a file op-3-4.php in the phpcourse/ folder and open it in Notepad.
  2. Paste the following script into it:
    \n";
        }
    ?>
    
  3. Save the file, but do not close it.
  4. See http://localhost/phpcursus/op-3-4.php for the result. Try to understand what is happening!
  5. Adjust the script so that it sends the table of 5, starting at 0 and ending at 100 to the browser.
  6. Compare the script with op-3-4.txt.

3.4. Functions

In a programming language, it is possible to write everything that is needed yourself, but in most languages the common tasks are greatly simplified by using functions. For example, there are functions that can count the number of characters in a string, or copy parts of strings. PHP already has a lot of functions baked in by default, and the XAMPP installation we use also has a lot of additional libraries installed (you can see this at http://localhost/xampp/phpinfo.php) with all sorts of extra functions.

The website www.php.net contains a good reference book of all common functions. It is mainly in Dutch, but occasionally some English words appear. They are divided into categories. The two examples mentioned above can be found via String Functions, and are explained on String Length and Substring.

The built-in functions can be used in this way:

";
echo "firstname: " . substr($name, 0, 4) . "
\n"; echo "lastname: " . substr($name, 5) . "
\n"; ?>

Task 3.5.

Check the result of the above script by creating a file in phpcourse/ and viewing it with your browser.