0% found this document useful (0 votes)
50 views10 pages

3.1 Conditions

Uploaded by

Markos Mathewos
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)
50 views10 pages

3.1 Conditions

Uploaded by

Markos Mathewos
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/ 10

Control Structure

MR. IRWIN C. CANSEJO & SISAY W.


Conditional Statements

Very often when you write code, you want to perform


different actions for different conditions. You can use
conditional statements in your code to do this.
In PHP we have the following conditional statements:
• if statement - executes some code if one condition is true
• if...else statement - executes some code if a condition is true
and another code if that condition is false
• if...elseif....else statement - executes different codes for more
than two conditions
• switch statement - selects one of many blocks of code to be
executed
PHP - The if Statement

The if statement executes some code if one


condition is true.

Syntax

if (condition) {
code to be executed if condition is true;
}
PHP - The if Statement

Example:
<?php
$grade = 50;

if ($grade > 50 ) {
echo “You are Passed";
}
?>
PHP - 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;
}
PHP - The if...else Statement

Example:
<?php
$grade = 50;

if ($grade > 50 ) {
echo "You are Passed";
} else {
echo "You are Failed";
}

?>
The if...elseif....else Statement

The if....elseif...else statement executes different codes


for more than two conditions.
Syntax

if (condition) {
code to be executed if condition is true;
} elseif (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
The if...elseif....else Statement

Example:
<?php
$grade = 80;

if ($grade >= 90 ) {
echo "You are Excellent";
} elseif ($grade >= 70 ) {
echo "You are Very Good";
} else {
echo "Please Study More";
}
?>
The PHP switch Statement

Use the switch statement to select one of many blocks of code to be


executed.

Syntax:
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
The PHP switch Statement

Example:

<?php
$grade = "A";

switch ($grade) {
case "A":
echo "You are Excellent";
break;
case "B":
echo "You are very Good";
break;
case "C":
echo "You are Good";
break;
default:
echo "Please Study More";
}
?>

You might also like