0% found this document useful (0 votes)
5 views16 pages

Ch4 Loop - Functions

Uploaded by

otrdthe6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Ch4 Loop - Functions

Uploaded by

otrdthe6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 4

Loop & Functions

By: Asst. Lect. Montadhar Moslem


PHP Loops
▸ Often when you write code, you want the same block of code to run over and over again a certain
number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.

▸ Loops are used to execute the same block of code again and again, as long as a certain condition is
true.

▸ In PHP, we have the following loop types:

• for - loops through a block of code a specified number of times

• 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

• foreach - loops through a block of code for each element in an array

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.

▸ This is how it works:

• expression1 is evaluated once

• expression2 is evaluated before each iteration


3
Example
▸ Print the numbers from 0 to 10:

▸ Example Explained

• The first expression, $x = 0;, is evaluated once and sets a counter to 0.

• 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.

▸ Example: Print $i as long as $i is less than 6:

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.

▸ Example: Print $i as long as $i is less than 6:

8
foreach Loop
▸ The most common use of the foreach loop, is to loop through the items of an array.

▸ Example: Loop through the items of an indexed 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.

o A function is a block of statements that can be used repeatedly in a program.

o A function will not execute automatically when a page loads.

o A function will be executed by a call to the function.

▸ 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 ():

▸ In our example, we create a function named myMessage().

▸ The opening curly brace { indicates the beginning of the function code, and the closing curly brace }
indicates the end of the function.

▸ The function outputs "Hello world!".


12
Function Arguments
▸ Information can be passed to functions through arguments. An argument is just like a variable.

▸ 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

You might also like