Ch4 Loop - Functions
Ch4 Loop - Functions
▸ Loops are used to execute the same block of code again and again, as long as a certain condition is
true.
• while - loops through a block of code as long as the specified condition is true
• do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true
2
for Loop
▸ The for loop - Loops through a block of code a specified number of times.
▸ The for loop is used when you know how many times the script should run.
▸ Example Explained
• The second expression, $x <= 10;, is evaluated before each iteration, and the code block is
only executed if this expression evaluates to true. In this example the expression is true as
long as $x is less than, or equal to, 10.
• The third expression, $x++;, is evaluated after each iteration, and in this example, the
expression increases the value of $x by one at each iteration. 4
The break Statement
▸ With the break statement we can stop the loop even if the condition is still true:
5
while Loop
▸ The while loop - Loops through a block of code as long as the specified condition is true.
6
while Loop
▸ The while loop does not run a specific number of times, but checks after each iteration if the
condition is still true.
▸ With the break statement we can stop the loop even if the condition is still true:
7
do while Loop
▸ The do...while loop - Loops through a block of code once, and then repeats the loop as long as the
specified condition is true.
8
foreach Loop
▸ The most common use of the foreach loop, is to loop through the items of an array.
▸ For every loop iteration, the value of the current array element is assigned to the variable $x. The
iteration continues until it reaches the last array element.
9
Keys and Values
▸ The array (in the previous slide) is an indexed array, where the first item has the key 0, the second
has the key 1, and so on.
▸ Associative arrays are different, associative arrays use named keys that you assign to them, and
when looping through associative arrays, you might want to keep the key as well as the value.
▸ This can be done by specifying both the key and value in the foreach definition, like this:
10
Functions
▸ The real power of PHP comes from its functions.
▸ PHP has more than 1000 built-in functions, and in addition you can create your own custom
functions.
▸ Besides the built-in PHP functions, it is possible to create your own functions.
▸ Create a Function: A user-defined function declaration starts with the keyword function, followed by
the name of the function:
11
Call a Function
▸ To call the function, just write its name followed by parentheses ():
▸ The opening curly brace { indicates the beginning of the function code, and the closing curly brace }
indicates the end of the function.
▸ Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.
▸ The following example has a function with one argument ($fname). When the familyName() function
is called, we also pass along a name, e.g. ("Jani"), and the name is used inside the function, which
outputs several different first names, but an equal last name:
13
Function Arguments
▸ The following example has a function with two arguments ($fname, $year):
14
Default Argument Value
▸ The following example shows how to use a default parameter. If we call the function setHeight()
without arguments it takes the default value as argument:
15
Functions - Returning values
▸ To let a function return a value, use the return statement:
16