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

M2S2 - S2 - PHP Control Structures

The document outlines PHP control structures, including decision-making statements like if, if...else, and switch, as well as looping structures such as while, do...while, and for. It provides syntax examples and explanations for each structure, emphasizing their roles in controlling the flow of program execution. Additionally, it includes practical code snippets to illustrate how these structures can be implemented in PHP.

Uploaded by

nightfury.000017
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)
2 views

M2S2 - S2 - PHP Control Structures

The document outlines PHP control structures, including decision-making statements like if, if...else, and switch, as well as looping structures such as while, do...while, and for. It provides syntax examples and explanations for each structure, emphasizing their roles in controlling the flow of program execution. Additionally, it includes practical code snippets to illustrate how these structures can be implemented in PHP.

Uploaded by

nightfury.000017
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/ 9

PHP: Control Structures

Outline

• { } Statement

• if…else

• if…elseif

• switch

• break

• while

• do…while

• for

Making Decisions
• Decision making or flow control is the process of determining the order in which statements
execute in a program

• The special types of PHP statements used for making decisions are called decision making
statements or decision-making structures

{ } Statement
• 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 ( } )

statement1;

statement2;

if Statements
• 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 (expression)

// do something

Example

<?php

if(isset($_GET["myNumber"])) {

$a = $_GET["myNumber"];


echo “ตัวเลขทีกรอก คือ ".$a;

if(a>10)

่ ค่ามากกวา่่ 10”;
echo “ตวัเลขทีกรอกมี

?>
if...else Statements
• 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:

if (expression)
// do something
else
// do another thing

• An if statement can be constructed without the else clause

• The else clause can only be used with an if statement

Example:

<?php
$a =1;
$b = 2;
if($a > $b)
echo “a is greater than b”;
else
echo “a is less than or equal to b”;
?>
• It executes another expression if the first fails

• The syntax for an if..elseif statement is:

if (expression1)
// do something
elseif(expression2)
// do another thing
elseif(expression3)
// do another thing

else // do another thing

Example

<?php
$a =19;
if($a == 1)
echo “one”;
elseif($a == 2)
echo “two”;
elseif($a == 3)
echo “three”;
elseif($a == 4)
echo “four”;
else
echo “more than four”;
?>

Switch Statements
• The same variable is compared with many different values

• The default statement is used if none of the cases are true

• Statements are executed until it sees a break statement

• The syntax for a switch statement is:

switch ($variable_name) {
case valueA:
statements;
break; // optional
case valueB:
statements;
break; // optional
default:
statements;
}

break
• break statement ends execution of the current for, while, do-while or switch structure.

• break accepts an optional numeric argument which tells it how many nested enclosing
structures are to be broken out of

Example

<?php
$a = 2;
switch($a){
case 0:
echo “a equals to 0”;
break;
case 1:
echo “a equals to 1”;
break;
default:
echo “a is greater than 1”;
}
?>

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> </meta>
</head>
<body>
<form action= "switch1.php" method= "GET">
Select a language:
<select name = "myLanguage" >
<option value= "TH"> Thailand </option>
<option value= "EN"> English </option>
<option value= "CH"> Chinese </option>
<option value= "JP"> Japanese </option>
</select>
<input type = "submit" value = "NO">
<hr>
</form>
<?php
if(isset($_GET["myLanguage"])) {
$x = $_GET["myLanguage"];
switch($x) {
case 'TH': echo 'Thailand' ; break;
case 'EN': echo 'English' ; break;
case 'CH': echo 'Chinese' ; break;
case 'JP': echo 'Japanese' ; break;
default: echo 'Invalid' ;
}}
?>
</body>
</html>

while
• It tells PHP to execute the nested statements repeatedly, as long as the while expression
evaluates to TRUE

• If the first evaluation of the statement return FALSE, the while loop will not be executed at all

• The syntax for while statement is:

while (expression){
statement;
statement;
}
Example

<?php
$x=1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>

do...while
• The statement inside the loop will be executed at least once.

• The truth expression is checked at the end of each iteration instead of in the beginning.

• The syntax for an do...while statement is:

do{
statement;
statement;
}while(expression);
<?php
$x=1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5 );
?>

Example

<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>

for
• for loop is used if you know how many times you want to execute the statements

• The syntax for for statement is:

for(initial;condition;inc/dec){
statement;
statement;
}

• The first expression(initial)is evaluated once unconditionally at the beginning of the loop

• In the beginning of each iteration, the second expression (condition)is evaluated. If the

result is True, the loop continues and the nested statements are executed. If the result is

False, the loop ends.

• At the end of each iteration, the third expression (increment/decrement) is evaluated.


Example

<?php
for($i = 1; $i < 10; $i++) {
echo "The number is $i ";
}
?>

You might also like