0% found this document useful (0 votes)
36 views14 pages

PHP2

Uploaded by

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

PHP2

Uploaded by

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

Program control statements

Outline
◊ Selection/ conditional statement
 If statement
 if...else statement
 if...else if....else statement
 switch statement
◊ Iterative Statement
 For loop
 While loop
 Do while
 For each
◊ Jump statement
 break
 Return
 Continue
1
Selection statement

If statement
The if statement executes some code if one condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
Example:
<?php The out put
$x=2; The condition is true

If($x>=2){
Echo “the condition is true”;
}
?>
2
The if...else Statement
 The if....else statement executes some code if a condition is true and another code if that condition
is false.
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false; The out put is
} This is executed if
condition is false
Example:
<?php
$x=1;
If($x>=2){
Echo “the condition is true”;
}
Else{
Echo “this is executed if condition is false”;}
?> 3
Other form of if..else
The out put
sythax the number is greater
( condition ) ? True_value : False_value than 2
Example:
<?php
$x=3;
echo ($x>2)?"the number is greater than 2":"the number is less than
two";
?>

4
Nested if statement
The if....else if...else statement executes different codes for more than
two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}

5
Con’t…
<?php
$result=45;
If($result>=90){
Echo “Your grade: A+”;
}
elseIf($result>=85){
Echo “Your grade: A”;
}elseIf($result>=80){
Echo “Your grade: A-”;
The out put is
}elseIf($result>=75){
Echo “Your grade: B+”; Your grade : F
}
else{
Echo “Your grade: F”;
}
6
?>
Switch statement
Use the switch statement to select one of many blocks of code to be executed.
Example:
Syntax <?php
switch ( expression ){$x=2;
switch ($x)
case value 1: {
statements case 1: The out put
echo "Number 1"; Number 2
break;
break;
… case 2:
case value n: echo "Number 2";
statements break;
case 3:
break; echo "Number 3";
default: break;
default:
statements echo "No number between 1 and 3";
} }
?>
?> 7
Iterative/loop statement
For loop
for - loops through a block of code a specified number of times
Syntax Example
for( initialization; condition; increment ){
<html>
//loop body <body>
<?php
} for ($i=1; $i<=5; $i++)
{
The out put echo "Hello World!<br />";
Hello World! }
Hello World! ?>
Hello World! </body>
Hello World! </html>
Hello World!

8
Con’t…
foreach - loops through a block of code for each element in an array
Synthax Example
foreach( array as [key=>]value ){ …
$arr =
loop body array(“name”=>”Abebe”,
} “dept”=>”IS”, “year”=>3,
The out put “cgpa”=>3.5);
name = Abebe
dept = IS
foreach( $arr as
year = 3 $key=>$value ){
cgpa = 3.5 echo $key . “ = “ .
$value . “<br>”;
}
9
Con’t…
while - loops through a block of code if and as long as a specified
condition is true example
Syntax <?php
while( condition ){ $i=1; The out put
while($i<=5)
loop body The number is 1
{ The number is 2
} echo "The number is " . $i . The number is 3
The number is 4
"<br />"; The number is 5
$i++;
}
?>
10
Con’t…
do...while - loops through a block of code once, and then repeats the
loop as long as the specified condition is true
Syntax Example
<?php
do { $x = 6; The out put
code to be executed; do { The number
} while (condition is true); echo "The number is: $x is: 6
<br>";
$x++;
} while ($x <= 5);
?>

11
Jump statement
break
 ends execution of the current for, foreach, while, do-while or switch
structure.
continue
 used within looping structures to skip the rest of the current loop iteration
 execution continues at the condition evaluation and then the beginning of
the next iteration
return
 If called from within a function, the return statement immediately ends
execution of the current function, and returns its argument as the value of
the function call
12
Logical operators
The PHP logical operators are used to combine conditional

?
statements.

13
Thank you
14

You might also like