Learnit Training

3. Programming

3.1. Variables

3.1.1. What is variable?

The memory of a computer is sometimes compared to a drawer cabinet, with drawers that can have a name 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 may not have the same name, which would cause confusion. However, it is not a problem if they have the same content. For example, the name of a drawer could be "place" and the content "Amsterdam". There is then a variable called "place" and has the value "Amsterdam". In PHP We write so on:

$plaats = "Amsterdam";

Note: The $ Character always represents the name of a variable. The = Character is used to assign a value; That which is to the right of the = sign 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). In PHP, each line is closed with ;.

We can always create a new tray. We can change the contents of trays. We can copy them. The contents of a drawer can be thrown at that of another. If the content is a number, we can even count on it!

  • New variable:
    $plaats = "Amsterdam";
    $voornaam = "Piet";
    $achternaam = "Jansen";
    There are now three variables with three different values.
  • Change value:
    $plaats = "Amsterdam";
    $plaats = "Rotterdam";
    First get the variable $plaats The value Amsterdam, this is changed on the second line to Rotterdam.
  • Copy value:
    $plaats = "Amsterdam";
    $woonplaats = $plaats;
    The value of the variable $plaats The second line is assigned to the variable $woonplaats.
  • Merge values:
    $voornaam = "Piet";
    $achternaam = "Jansen";
    $naam = $voornaam. " " . $achternaam;
    The variable $naam Gets the value "Piet Jansen".
  • Numbers:
    $getal 1 = 20;
    $getal 2 = 5;
    $uitkomst = $getal 1 + $getal 2;
    The variable $uitkomst Gets the value 25.

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

Note: In the previous chapter, We already saw that with the assignment Echo Which is being sent out to the export. In our case, this always means that everything behind Echo is in the Web browser.

Note: PHP files always start with a line on which State. They always end with a line on which ?> State. All other lines always end with ;.

Contract 3.1.

  1. In the folder Phpcursus/ Create a file on-3-1. PHP and open it in Notepad, always run the following commands on a new line.
  2. Make sure the file starts and ends correctly.
  3. Create a new variable named and have the value Klaas (in the aforementioned string notation)
  4. Send the value of name to the Webbrower.
  5. Change the value of name to Henk.
  6. Again, send the value of name to the Webbrower.
  7. View the result on http://localhost/phpcursus/op-3-1.php
  8. If it is good, the result is a window in which Klaashenk (without space or enter) is printed
  9. Consider a way to get the string "
    (The XHTML code for a new line) between Klaas And Henk To get.
  10. Check the result again to see if this is successful.
  11. Compare the contents of the file you created with the contents of op-3-1. txt

    Note: The file with which it is compared has the extension . txt, this to prevent the content being INTERPRETED as PHP and so you could no longer see what the code was.

3.1.2. Other types of variables

So far, without really knowing, we have already used different types of variables, namely strings and integers. The following is a complete list of the available variable types in PHP, with always examples of the values that the variables can contain.

  • Integer: Integers
    $a = 3;
    $b =-16;
    $c = 130;
  • Double: Double precision
    $a = 0.9;
    $b =-19,734;
    $c = 100,001;
  • Float: Floating
    $a = 4e-15;
    $b = 6.000000000001;
    $c = 15.1;
  • Boolean: Boolean
    $a = True;
    $b = False;
  • Char: Characters
    $a = ' B ';
    $b = ' 6 ';
    $c = ' # ';
  • String: String
    $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";
    Note: Note that $b And $c The same.

3.2. Conditions

In almost every PHP script, it will be necessary to see if variables meet certain conditions: has a certain date already been, is the password entered, is one number larger than the other?

The structure of such conditions looks like:

  • If a condition is true
    • Run this if the condition is true
  • Else
    • Run 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";

Note: Note The variables names above a Hyphen, _, it is common to do so if the name of a variable consists of 2 parts. Furthermore, the Greater than Character >, used to compare the values of 2 variables.

Contract 3.2.

  1. In the phpcursus folder, create a file on-3-2. PHP and open it in Notepad.
  2. Paste the code from the example above into the file.
  3. Consider what the outcome of the script should be.
  4. Check this by browsing to http://localhost/phpcursus/op-3-2.php .
  5. Adjust the ages of Piet and Jan so that Piet is younger than Jan and look again at the result.

3.2.1. Comparison operators

In The example above, we used the Greater than Character >, to compare two variables. The > Sign is a comparison operator. The idea of such an operator is that the variables are compared on two sides. 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

Contract 3.3.

  1. In the phpcursus folder, create a file on-3-3. PHP 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 not always equal to 2).
  6. This result is already completed in the table below. Make this table (on paper) by:
    • The values of $getal 1 And $getal 2 To change.
    • Change the comparison operator.
    • Save the changes.
    • The Web browser with the result always refresh (F5).
  7. Table 3.2. Practice with comparison operators
    $getal 1Operator$getal 2Outcome

3.3. Loops

So far, programming has only cost us more time. We could have just untrue in a file, instead of the much more complicated codes that yield the same result. And even though what we have already learned will prove important, the use of loops is the 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 only discuss the for loop. The other two, while Loop and do.. While loop are only variations to this.

3.3.1. For Loop

Suppose we want to get the numbers 1 through 20 on the screen, we can just type them. With twenty numbers it still goes, as soon as it is more 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:


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

Or in words:

  1. The variable $i is on 1 (initialized).
  2. It verifies that the value of $i (1 In this case), it is smaller than 20.
  3. If this is the case, the line $i. "
    In this case) sent to the browser.
  4. The variable $i becomes with 1 Increased (by $i + +), thus being 2.
  5. It verifies 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 becomes with 1 Incremented.
  8. Steps 5 through 7 are repeated until $i Equal to 20.

The 3th line is simply executed 20 times.

Contract 3.4.

  1. In the phpcursus folder, create a file on-3-4. PHP 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-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. Features

It is possible to write everything that is needed in a programming language, but in most languages the common tasks are very simplified using functions. For example, there are functions that can count the number of characters in a string, or copy pieces from strings. In PHP, a lot of features have been ingrained by default and the XAMPP installation we use also has many additional libraries installed (you can see this on http://localhost/xampp/phpinfo.php) with all the extra features.

On the website www.php.net is a good reference work of all common functions to be found. It is mainly in Dutch, but occasionally some English words come in. They are divided into catagories. The two examples listed above are found via string Functions, and are Explained at string length and substring.

The ingrained functions can be used:

\n "; echo "FirstName:". substr ($naam, 0, 4). "
\n "; echo "Surname:". SUBSTR ($naam, 5). "
\n "; ?>

Contract 3.5.

Check the result of the above script, by creating your own file in Phpcursus/ and viewing it with the browser.