Using conditional statements September 20, 2008
Posted by me2blog in Basic PHP programming, php.Tags: conditional statements in PHP, conditions in PHP, if statement in PHP, php, switch statement in PHP
trackback
Conditional statements executes a block of statements only when certain conditions r true.
1. Using If statement:
The general format of and if conditional statement is as follows:
if (condition)
{
block of statements
}
elseif (condition)
{
block of statements
}
else
{
block of statements
}
Explanations:
- if: The block of statements executes if the condition is true only and after executing it, the script moves to the next instruction skipping any elseif or else statements (only 1 part will execute). If the condition is false then it will move to line below the curly braes, whatever it’s elseif or else or another statement.
- elseif: This section is optional, u can use more than one elseif, they executes in the same way as the above if
- else: This section is also optional, only one else statement is allowed and this part doesn’t test any condition bcz PHP consider that when the script reach that point then no other statments above was executed and thus the else condition will be true.
Example:
we want to print the student grade with a message and with a grade symbol, like the following:
| grade range | Message | grade symbol |
| 99-89 | Excellent | A |
| 90-79 | Very good | B |
| 80-69 | Good | C |
| 70-59 | O.K | D |
| 60-49 | Poor | E |
| less than 50 | Failed! | F |
<?php
$grade_check= 75;
if ($grade_check<100 and $grade_check >89)
{
$grade= “A”;
$message = “Excellent”;
}
elseif ($grade_check <90 and $grade_check > 79)
{
$grade= “B”;
$message = “Very good”;
}
elseif ($grade_check <80 and $grade_check > 69)
{
$grade= “C”;
$message = “Good”;
}
elseif ($grade_check <70 and $grade_check >59)
{
$grade= “D”;
$message = “O.K”;
}
elseif ($grade_check <60 and $grade_check> 49)
{
$grade= “E”;
$message = “Poor”;
}
else
{
$grade= “F”;
$message = “Failed!”;
}
echo “for the student grade = “, $grade_check, “<br>”,$message, “ ( “,$grade, ” )<br>”;
?>
The output for three random grades:
for the student grade = 75
Good ( C )
for the student grade = 64
O.K ( D )
for the student grade = 95
Excellent ( A )
Note1: U can test 4 negative if statements that is the block of statements r executed when the condition is false, by adding exclamation point (!) at the beginning of the condition. i.e. if( ! $number >5) then the block of statements will execute only if the $number contains value less than or equal 5.
Note2: U can have nested if statement by adding if statement inside another if, and so on. this is called nesting. but remember that u can’t reach the inner if , if the outer if isn’t true … the block of statements will execute only if all the if’s r true in the path till reach that block!
1. Using Switch statement:
switch statement is a good option when u ‘ve a lot of conditions. The switch statement tests the value of one variable and then executes the block of variables that matching the value in the variable.
switch ($variable_name)
{
case value:
{
Block of statements
}
case value:
{
Block of statements
}
.
.
.
default:
{
Block of statements
}
}
- The default section is optional, works like else in the if statements, and it will exucute only it the variable value didn’t match any case statement, u can place it anywhere but it make more sense to add it at the end of the switch statement.
- The break statements r essentials, if u didn’t write it, if the case if true it will contiue to execute all the cases below it without checking the condition.
Example:
the same as the above example but we will write it using switch
<?php
$grade_check_s= 48;
switch ($grade_check_s)
{case ($grade_check_s <100 and $grade_check_s >89):
{
$grade_s= “A”;
$message_s = “Excellent”;
break;
}
case ($grade_check_s <90 and $grade_check_s >79):
{
$grade_s= “B”;
$message_s = “Very good”;
break;
}
case ($grade_check_s <80 and $grade_check_s >69):
{
$grade_s= “C”;
$message_s = “Good”;
break;
}
case ($grade_check_s <70 and $grade_check_s >59):
{
$grade_s= “D”;
$message_s = “O.K”;
break;
}
case ($grade_check_s <60 and $grade_check_s >49):
{
$grade_s= “E”;
$message_s = “Poor”;
break;
}
default:
{
$grade_s= “F”;
$message_s = “Failed!”;
}
}
echo “for the student grade = “, $grade_check_s, “<br>”,$message_s, “ ( “,$grade_s, ” )<br>”;
?>
The output for three random grades:
for the student grade = 48
Failed! ( F )
for the student grade = 86
Very good ( B )
for the student grade = 77
Good ( C )

Comments»
No comments yet — be the first.