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

Control-Statements Lecture

The document discusses various PHP control statements including if, if-else, if-else-if, nested if, and switch statements. It provides the syntax and flowcharts for each statement and includes examples of how each statement can be used. The if statement allows conditional execution of code if a condition is true. The if-else statement executes one block of code if the condition is true and another if false. The if-else-if statement allows checking multiple conditions. Nested if statements contain if blocks within other if blocks. Finally, the switch statement allows performing different actions based on different conditions.

Uploaded by

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

Control-Statements Lecture

The document discusses various PHP control statements including if, if-else, if-else-if, nested if, and switch statements. It provides the syntax and flowcharts for each statement and includes examples of how each statement can be used. The if statement allows conditional execution of code if a condition is true. The if-else statement executes one block of code if the condition is true and another if false. The if-else-if statement allows checking multiple conditions. Nested if statements contain if blocks within other if blocks. Finally, the switch statement allows performing different actions based on different conditions.

Uploaded by

Bonsh Chan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Control Statements

PHP If Else
PHP if else statement is used to test condition. There are various ways to use if statement
in PHP.

o if
o if-else
o if-else-if
o nested if

PHP If Statement
PHP if statement allows conditional execution of code. It is executed if condition is true.

If statement is used to executes the block of code exist inside the if statement only if the
specified condition is true.

Syntax Flowchart

Example:
Output:

PHP If-else Statement


PHP if-else statement is executed whether condition is true or false.

If-else statement is slightly different from if statement. It executes one block of code if the
specified condition is true and another block of code if the condition is false.

Syntax

Flowchart: Example:

Output:
PHP If-else-if Statement
The PHP if-else-if is a special statement used to combine multiple if .else statements. So,
we can check multiple conditions using this statement.

Syntax

Flowchart: Example:

<?php
$num=4;
if($num==5) {
echo "Five Fish";
}
else if($num==4) {
echo "Four Birds";
}
else if($num==3) {
echo "Three Monkeys";
}
else if($num==2) {
echo "Two Ducks";
}
else {
echo "A Lion";
Output: }
?>

PHP nested if Statement


The nested if statement contains the if block inside another if block. The inner if statement executes
only when specified condition in outer if statement is true.

Syntax

Flowchart: Example:

Output:
PHP switch Statement
• The switch statement is used to perform different actions based on different conditions.
• Use the switch statement to select one of many blocks of code to be executed.
Syntax

Example:
Output:

Exercises:
1. Create a program if entered password (via Form) is correct;

If correct=”Successful!”
Else “Password Incorrect.”

1. Create a program to enter a score via form, and display the appropriate remark
Score=101; invalid score
Score=80-100; Good
Score=50-79; Poor
Others; Fail

You might also like