Learnit Training

3. Programming

3.1. Variables

3.1.1. What is a variable?

The memory of a computer is sometimes compared to a chest of drawers, 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 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 the contents "Amsterdam". There is then a variable called "place" that has the value "Amsterdam". In PHP we write it down like that:

$place = "Amsterdam";

Note: The $ sign is always before the name of a variable. The = character is used to assign a value; that which is 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 content 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"in the second line it is changed to "Rotterdam".
  • Copy value:
    $place = "Amsterdam";
    $residence = $place;
    
    The value of the variable $place is assigned in the second line to the variable $residence.
  • Merge values:
    $first name = "Piet";
    $last name = "Jansen";
    $name = $firstname . " " . $lastname ;
    
    The variable $name is given the value "Pete Jansen".
  • Numbers:
    $number1 = 20;
    $number2 = 5;
    $outcome = $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 equalization with ^.

Note: We already saw in the previous chapter that with the assignment 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.

Note: PHP files always start with a line where <?php state. They always end with a line that says ?> state. All other rules always end with ;.

Task 3.1.

  1. Create a file op-3-1.php in the phpcourse/ folder and open it in Notepad, run the commands below on a new line each time.
  2. Make sure the file starts and ends correctly.
  3. Create a new variable called name and with the value klaas (in the string notation mentioned earlier)
  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. View the result on http://localhost/phpcursus/op-3-1.php
  8. If all goes well, the result will be a window with the klaashenk (without space or enter) printed on it
  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 worked.
  11. Compare the content of the file you 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 not 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: Integer 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";
    
    Note: 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
    • Do this if the condition is true
  • Otherwise
    • Do 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 variable names above a horizontal dash, _It is customary to do so when the name of a variable consists of 2 parts. Furthermore, the above 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 example above into the file.
  3. Think about what the outcome of the script should be.
  4. Check this by visiting 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 above 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
operatorrepresents
>above
<lower than
>=greater than or equal to
<=less than or equal to
==equal to
!=unlike

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 to 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$number2outcome
    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 Onwaar in a file, instead of the much more complicated codes that give the same result. And even though what we have learned so far will turn out 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, then we can just type them in. Twenty numbers is fine, but as soon as more numbers 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 will be on 1 set (initialized).
  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 elevated (by $i++), and thus 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 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. Modify 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

It is possible in a programming language 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 extra libraries installed (you can see this at http://localhost/xampp/phpinfo.php) with all kinds of extra functions.

On the website www.php.net you can find a good reference book of all common functions. It is mainly in Dutch, but occasionally there are some English words in it. 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 view it with your browser.