Cory
Communication Traffic Observer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
PHP If…Else Statements
The if statement is used to execute code with condition. If the value of the condition is True then execute the statement else execute another statement or simply end the execution.
There are three combination of the If statement namely:
The if statement
Execute the code if the condition is true.
If Syntax
or
The first syntax is used if only one line of code is to be executed after the condition.
The second syntax is used to execute multiple lines of code.
Consider the following example for the first syntax:
Second syntax example:
The if…else statement
The above code will execute only if the condition is true. The If…Else statement is able to execute another statement if the condition is false.
If…Else Syntax
The sample below will output the correct month if the condition is false:
The function date with “F” parameter will simply output the month in full textual representation like January or February.
Take note that in order to execute multiple statements you must enclosed it with curly braces.
The if...elseif....else Statement
Use this statement if you need more condition aside from single condition.
if…elseif…else statement
The example code below will try to execute the next condition if the first condition is false. Otherwise execute the statement in the “else” statement.
The if statement is used to execute code with condition. If the value of the condition is True then execute the statement else execute another statement or simply end the execution.
There are three combination of the If statement namely:
- if statement – only one condition is executed.
- if...else statement – only one condition is executed but it will execute another statement if the condition is false.
- if...elseif....else statement – it can contain more than two conditions and execute a statement if the condition is true otherwise execute the statement using the else.
The if statement
Execute the code if the condition is true.
If Syntax
- if
(
condition)
statement to be executed if
condition is true
;
or
- if
(
condition)
- {
- statement to be executed if
condition is true
;
- Another statement to be executed if
the condition is true
;
- }
The first syntax is used if only one line of code is to be executed after the condition.
The second syntax is used to execute multiple lines of code.
Consider the following example for the first syntax:
- <?php
- $month
=
date
(
"M"
)
;
- if
(
$month
==
"Aug"
)
echo
"The month is August!"
;
- ?>
Second syntax example:
- <?php
- $month
=
date
(
"M"
)
;
- if
(
$month
==
"Aug"
)
- {
- echo
"The month is August!"
;
- echo
"Have a nice day!"
;
- }
- ?>
The if…else statement
The above code will execute only if the condition is true. The If…Else statement is able to execute another statement if the condition is false.
If…Else Syntax
- if
(
condition)
- statement to be executed if
condition is true
;
- else
- statement to be executed if
condition is false
;
The sample below will output the correct month if the condition is false:
- <?php
- $month
=
date
(
"M"
)
;
- if
(
$month
==
"Sep"
)
- {
- echo
"The month is September!"
;
- }
else
{
- echo
"The month is "
.
date
(
"F"
)
;
- }
- ?>
The function date with “F” parameter will simply output the month in full textual representation like January or February.
Take note that in order to execute multiple statements you must enclosed it with curly braces.
The if...elseif....else Statement
Use this statement if you need more condition aside from single condition.
if…elseif…else statement
- if
(
condition)
- statement to be executed if
condition is true
;
- elseif
(
condition)
- statement to be executed if
condition is true
;
- else
- statement to be executed if
condition is false
;
The example code below will try to execute the next condition if the first condition is false. Otherwise execute the statement in the “else” statement.
- <?php
- $month
=
date
(
"M"
)
;
- if
(
$month
==
"Sep"
)
- echo
"The month is September!"
;
- elseif
(
$month
==
"Oct"
)
- echo
"The month is October!"
;
- else
- echo
"The month is "
.
date
(
"F"
)
;
- ?>