0% found this document useful (0 votes)
8 views

Lesson 8 PHP-Control Statements and Functions

Uploaded by

olivertech
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lesson 8 PHP-Control Statements and Functions

Uploaded by

olivertech
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

PHP Control Statements

& Functions
Lecturer: Mr. Nevy
EMAIL: [email protected]
Outline:
2

 PHP Decision making


🞑 if
🞑 if…else
🞑 if…else...elseif
🞑 switch…case statements
 PHP Loops
🞑 While
🞑 Do…While
🞑 For
🞑 Foreach
2
Statement
s
4

 Used to execute specific programming


code if the evaluation of a conditional
expression returns a value of TRUE
 The syntax for a simple if statement is:
if (conditional expression)
statement;

3
Conditional
Expression
5

4
if Statements
(continued)
6

 Contains three parts:


🞑 the keyword if
🞑 a conditional expression enclosed within
parentheses
🞑 the executable statements
 A command block is a group of
statements contained within a set of
braces
 Each command block must have an
opening brace (
{ ) and a closing brace ( } )
5
if Statements
(continued)
6

6
if Statements
(continued)
7

7
if...else
Statements
8

 An if statement that includes an else


clause is called an if...else
statement
 An else clause executes when the
condition in an
if...else statement evaluates to
FALSE
 The syntax for an if...else

statement is:

8
if...else Statements
(continued)
9

 An if statement can be constructed


without the
else clause
 The else clause can only be used with
an if
statement

9
if...else Statements
(continued)
9

10
if…else…elseif
Statements
10

 When we want to execute different code for


different set of conditions, and we have more
than two possible conditions, then we use
if...elseif...else

11
if…else…elseif
Statements
10

12
switch
Statements
11

 Control program flow by executing a specific


set of statements depending on the value
of an expression
 Compare the value of an expression to a
value contained within a special
statement called a case label
 A case label is a specific value that contains
one or more statements that execute if
the value of the case label matches the
value of the switch
statement’s expression
13
switch Statements
(continued)
12

 Consist of the following


components:
🞑 The switch keyword
🞑 An expression

🞑 An opening brace
🞑 One or more case labels

🞑 The executable statements

🞑 The break keyword

🞑 A default label

🞑 A closing brace
14
switch Statements
(continued)
13

 The syntax for the switch


statement is:

15
switch Statements
(continued)
14

 A case label consists of:


🞑 The keyword case
🞑 A literal value or variable name

🞑 A colon (:)
 A case label can be followed by a
single statement or multiple
statements
 Multiple statements for a case label do
not need to be enclosed within a
command block
16
switch Statements
(continued)
15

 The default label contains statements that


execute when the value returned by the
switch statement expression does not
match a case label
 A default label consists of the keyword
default followed by a colon (:)

17
Examp
le:
16

18
Loop
Statement
17

 A loop statement is a control structure that


repeatedly executes a statement or a
series of statements while a specific
condition is TRUE or until a specific
condition becomes TRUE
 There are four types of loop statements:
🞑 while statements
🞑 do...while statements

🞑 for statements

🞑 foreach statements
19
while
Statements
18

 Tests the condition prior to executing the


series of statements at each iteration of
the loop
 The syntax for the while statement is:

 As long as the conditional expression


evaluates to TRUE, the statement or
command block that follows executes
repeatedly 20
while Statements
(continued)
19

 Each repetition of a looping statement is called


an
 iteration
 A while statement keeps repeating until

its conditional expression evaluates to


FALSE
 A counter is a variable that increments or

decrements with each iteration of a loop


statement
21
while Statements
(continued)
20

22
do...while
Statements
21

 Test the condition after executing a series


of statements then repeats the execution
as long as a given conditional expression
evaluates to TRUE
 The syntax for the do...while
statement is:

23
do...while Statements
(continued)
22

 do...while statements always execute


once, before a conditional expression is
evaluated

24
do...while Statements
(continued)
23

25
for
Statements
24

 Combine the initialize, conditional


evaluation, and update portions of a
loop into a single statement
 Repeat a statement or a series of
statements as long as a given conditional
expression evaluates to TRUE
 If the conditional expression evaluates to
TRUE, the for statement executes and
continues to execute repeatedly until the
conditional expression evaluates to FALSE
26
for Statements
(continued)
25

 Can also include code that initializes a


counter and changes its value with each
iteration
 The syntax of the for statement is:

27
for Statements
(continued)
26

28
foreach
Statements
27

 Used to iterate or loop through the


elements in an array
 Do not require a counter; instead, you
specify an array expression within a set
of parentheses following the foreach
keyword
 The syntax for the foreach statement
is:

29
foreach Statements
(continued)
28

30
PHP - Functions
 PHP provides us with two major types of functions:
Built-in functions & User Defined Functions.
 Built-in functions provides us with a huge collection
of built-in library functions. These functions are
already coded and stored in form of functions. To use
those we just need to call them as per our
requirement like, var_dump, fopen(), print_r(),
gettype() etc.
 The real power of PHP comes from its functions; it
has more than 1000 built-in functions.

31
PHP - Functions
 PHP User Defined Functions
 Apart from the built-in functions, PHP allows
programmers to create customised functions
called the user-defined functions.
 A function is a block of statements that can be
used repeatedly in a program.
 A function will not execute immediately when a
page loads.
 A function will be executed by a call to the
function.

32
Why should we use functions?
 Reusability: If we have a common code that we would like
to use at various parts of a program, we can simply contain it
within a function and call it whenever required. This reduces
the time and effort of repetition of a single code. This can be
done both within a program and also by importing the PHP
file, containing the function, in some other program
 Easier error detection: Since, our code is divided into
functions, we can easily detect in which function, the error
could lie and fix them fast and easily.
 Easily maintained: As we have used functions in our
program, so if anything or any line of code needs to be
changed, we can easily change it inside the function and the
change will be reflected everywhere, where the function is
called. Hence, easy to maintain.
33
Creating a User Defined
Function in PHP
 A user defined function declaration starts with the word
keyword "function":
 Syntax:
function functionName() {
code to be executed;
}
 Note: A function name can start with a letter or underscore
(not a number) and ends with an open and closed parenthesis.
 To call a function we just need to write its name followed by the
parenthesis
 Programmers should assign function names that reflect what
the function does.
 Function names are NOT case-sensitive.

34
Example: Creating a function named
"writeMsg()"

 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!".
 To call the function, just write its name:

35
Function Parameters/Arguments
 The information or variable, within the function’s
parenthesis, are called parameters. They are used to
hold the values executable during runtime.
 A programmer can take in as many parameters as he
wants, separated with the comma(,) operator.
 Parameters are used to accept inputs during runtime.
While passing the values like during a function call, they
are called arguments.
 An argument is a value passed to a function and a
parameter is used to hold those arguments. In common
term, both parameter and argument mean the same. We
need to keep in mind that for every parameter, we need
to pass its corresponding argument.
36
Function Parameters/Arguments
 Syntax:
function function_name($first_parameter,
$second_parameter) {
executable code;
}

Example Code:
<?php
// function along with three parameters
function proGeek($num1, $num2, $num3) {
$product = $num1 * $num2 * $num3;
echo "The product is $product";
}
// Calling the function & Passing three arguments
proGeek(2, 3, 5);
?>

37
Setting Default Values for Function
Parameter
 PHP allows us to set default argument values for function
parameters. If we do not pass any argument for a parameter with
default value then PHP will use the default set value for this
parameter in the function call.

 Example Code:
<?php
// function with default parameter
function defAge($str, $num=12) {
echo "$str is $num years old \n";
}
// Calling the function
defAge(“Jane", 15);
// In this call, the default value 12 will be
considered
defAge("Adam");
?>
38
Returning Values from Functions
 Functions can also return values to the part of
program from where it is called.
 The return keyword is used to return value back
to the part of program, from where it was called.
 The returning value may be of any type
including the arrays and objects.
 The return statement also marks the end of the
function and stops the execution after that and
returns the value.

39
Returning Values from Functions
<?php
 Example Code:
//function along with three
parameters
function proThree($num1, $num2,
$num3) {
$product = $num1 * $num2 *
$num3;
return $product; //returning the
product
}
//storing the returned value
$retValue = proThree(2, 3, 5);
echo "The product is $retValue";
?> 40
Returning Values from Functions
PHP allows two ways in which an argument can be
passed into a function:
(1) Pass by Value: On passing arguments using pass by
value, the value of the argument gets changed within a
function, but the original value outside the function
remains unchanged. That means a duplicate of the
original value is passed as an argument.
(2) Pass by Reference: On passing arguments as pass
by reference, the original value is passed. Therefore, the
original value gets altered. In pass by reference we
actually pass the address of the value, where it is stored
using ampersand sign (&).
41
Returning Values from Functions
<?php
// pass by value
function valPass($num) {
$num += 2;
return $num;
}
// pass by reference
function refPass(&$num) {
$num += 10;
return $num;
}
$n = 10;
valPass($n);
echo "The original value is still $n \n";
refPass($n);
echo "The original value changes to $n";
?>
42

You might also like