Control-Statements Lecture
Control-Statements Lecture
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:
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: }
?>
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