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

Lect 4

The document discusses different control structures in PHP including conditional structures like if, if/else, if/elseif/else and switch statements. It also covers iterative structures like while, do/while, for loops and the break statement. Conditional structures allow executing code based on variable conditions, while iterative structures allow repeating code execution.

Uploaded by

Musa Mahdi
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)
47 views

Lect 4

The document discusses different control structures in PHP including conditional structures like if, if/else, if/elseif/else and switch statements. It also covers iterative structures like while, do/while, for loops and the break statement. Conditional structures allow executing code based on variable conditions, while iterative structures allow repeating code execution.

Uploaded by

Musa Mahdi
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/ 11

Control Structures

Lecture # 4
Control Structure
• In this chapter we have two different types of structures.
Conditional Structure
Iterative Control Structure.
• Conditional structure describe the if-then, if-then-else, if-then-else- if-then-
else, and switch statements. These structure enable you to make decisions
based on single variables or collections of variables.
PHP - The if Statement
• <?php
$t = date("H");
• The if statement is used to
execute some code only if a if ($t < "20") {
specified condition is true. echo "Have a good day!";
• Syntax: }
?>
if (condition) {
code to be executed if condition is true;
}
• <?php
PHP - The if...else Statement
$t = date("H");

• Use the if....else statement to execute if ($t < "20") {


some code if a condition is true and echo "Have a good day!";
another code if the condition is false.
• Syntax
} else {
if (condition) {
echo "Have a good night!";
code to be executed if condition is true; }
} else {
code to be executed if condition is false; ?>
}
• <?php
PHP - The if...elseif....else
$t = date("H");
Statement
if ($t < "10") {
• Use the if....elseif...else statement to specify echo "Have a good morning!";
a new condition to test, if the first
condition is false. } elseif ($t < "20") {
• Syntax echo "Have a good day!";
• if (condition) { } else {
code to be executed if condition is true;
} elseif (condition) {
code to be executed if condition is true;
echo "Have a good night!";
} else {
code to be executed if condition is false;
}
} ?>
PHP Switch Statement
• The switch statement accepts one formal parameter, which should be either
an integer, float or string primitive variable.
• The break statement is required in each case statement to signal that the
evaluation has found a match and should exit the switch statement.
• If there is no break statement in a case, the program will fall through once it
has found a match until it runs the default case statements.
• <?php
$favcolor = "red";

The PHP switch Statement switch ($favcolor) {


case "red":
echo "Your favorite color is red!";
break;
• Use the switch statement to select one of many blocks of case "blue":
code to be executed. echo "Your favorite color is blue!";
• switch (n) {
case label1:
break;
code to be executed if n=label1;
break;
case "green":
case label2:
code to be executed if n=label2;
echo "Your favorite color is green!";
break; break;
case label3:
code to be executed if n=label3; default:
...
break; echo "Your favorite color is neither red, blue,
default:
code to be executed if n is different from all labels;
or green!";
} }
?>
PHP While Statement • <?php
$i = 1;
• While loops are the simplest type of loop in PHP.
They behave just like their Ccounter parts. The basic while ($i <= 10) {
form of a while statement is:
echo $i;
• Syntax:-
while ( expression ) { $i++;
// do something }
• The meaning of a while statement is simple. It tells
}
PHP to execute the nested statement(s) repeatedly, as
long as the while expression evaluates to TRUE. The
value of the expression is checked each time at the
beginning of the loop, so even if this value changes
during the execution of the nested statement(s),
execution will not stop until the end of the iteration
(each time PHP runs the statements in the loop is one
iteration).
Do.. While Statement • <?php
$num = 1;
do {
• Do..while loops are very similar to while
loops, except the truth expression is checked
echo"Execution number:
at the end of each iteration instead of in the $num<br>\n";
beginning. The main difference from regular
while loops is that the first iteration of $num++;
a do..while loop is guaranteed to run (the truth
expression is only checked at the end of the } while ( $num > 200 && $num < 400 );
iteration), whereas it's may not necessarily run
with a regular while .
?>
• Syntax:-
• do {
// code to be executed
} while ( expression );
• <?php
/* example 1 */

for ($i = 1; $i <= 10; $i++) {


For Statement echo $i;
}
• For loops are the most complex loops in PHP. They
behave like their Ccounterparts. The syntax of a for loop
is:.
/* example 2 */
• Syntax:-
• for ( initialization expr1; test expr2; modification expr3 )
{ // code to be executed } for ($i = 1; ; $i++) {
• The first expression (expr1) is evaluated (executed) once
unconditionally at the beginning of the loop.
if ($i > 10) {
• In the beginning of each iteration, expr2 is evaluated. If it break;
evaluates to TRUE, the loop continues and the nested
statement(s) are executed. If it evaluates toFALSE, the
execution of the loop ends.
}
• At the end of each iteration, expr3 is evaluated (executed). echo $i;
• Each of the expressions can be empty. expr2 being empty
means the loop should be run indefinitely (PHP implicitly }
considers it as TRUE, like C). This may not be as useless
as you might think, since often you'd want to end the loop
using a conditional break statement instead of using the for
truth expression.
• <?php
echo "<p><b>Example of using the
Break statement Break statement:</b></p>";

• Break ends execution of the current for ($i=0; $i<=10; $i++) {


for, for each, while, do..while or
switch structure. Break accepts an
if ($i==3){break;}
optional numeric argument which echo "The number is ".$i;
tells it how many nested enclosing echo "<br />";
structures are to be broken out of.
}
?>

You might also like