PHP - Conditions
Hello All...
welcome every one. I'm going to start PHP lectures for Very Beginners, who
wants to learn PHP from very beginning. No matter if you do not have any basic
programming concept. If you follow my tutorials I hope you will surely get some
how good knowledge of PHP.
Today our
Topic is
Conditional Statements
Conditional
Statements are set of commands used to perform different actions based on
different conditions. These are used to control flow of code by applying
different types of conditions. In PHP we have following conditional statements.
i-
If
ii-
Else
iii-
Else if
iv-
Switch
i-
IF: We use this condition only when a specified
condition is true. If the condition is true then do that thing. Here is simple
syntax.
If (condition) { // start
of if body
execute the statement
} // end of if body
Let’s take an example
<?php
$num1 = 10;
$num2 = 20;
if( $num1 < $num2 ){
echo “10 is less than 20”;
}
?>
ð If you want to
print multiple statements inside the if condition then you can also echo
another statement within the if body.
<?Php
$num1 =
10;
$num2 = 20;
if( $num1 < $num2 ){
echo “10 is less than 20”;
echo “<br />”;
echo “20 is greater than 10”;
}
?>
ð `<br />’
will bring the next echo statement in new line.
ð So try to use
<br /> if you are printing multiple statements
ii-
Else: If you want to execute some code if condition is
true and another code if the condition is false then you must use else
statement.
Let’s take above example and make simple changing in
condition.
<?Php
$num1 =
10;
$num2 = 20;
if( $num1 > $num2 ){
echo “10 is less than 20”;
echo “<br />”;
echo “20 is greater than 10”;
}
else{
echo
“10 is less than 20”;
}
?>
In condition we are saying that if 10 is greater than 20 then
print these statements but the condition is false, so this block/part of code
will be skipped and will be moved to else statement and will be printed this
else statement. Let’s look for another example.
<?php
$num = 15;
If($num >= 10 && $num <= 20){
echo “the number is between 10 and 20”;
}
else{
echo “number is something else”;
}
?>
In above example we have checked two conditions using AND
(&&) Operator that either the number is greater than 10 or less than 20
then print if statement otherwise print else statement.
iii-
Else-If: This condition is used when we know that
there would be any one out of the multiple statements is true, rest of the
others are false. Else if is known as the extension of if structure. If one
condition fails then it executes another `if` condition.
Let’s look
for an example of simple Grading System
<?php
$per = 87;
if( $per >= 90 && $per
< 100 ){
echo "You got A+
Grade";
}
elseif($per < 90 && $per
>=80){
echo "You got A grade";
}
elseif($per < 80 && $per
>=70){
echo "You got B
grade";
}
elseif($per < 70 && $per
>=60){
echo "You got C
grade";
}
elseif($per < 60 && $per
>=50){
echo
"Improve";
}
else{
echo "You are
fail";
}
?>
Below is the code and result of else if example.
In last it’s
good to include else statement if any of the above conditions are false then
else will be executed.
i-
Switch: Switch statement is used to compare a variable
and/or an expression to different values. This works like if-else statement
there is no any main difference between if-else statement and switch statement
the only difference is of syntax.
Here is the
syntax
switch (
<variable> ) {
case this-value:
Code to execute if
<variable> == this-value
break;
case that-value:
Code to execute if
<variable> == that-value
break;...
default:
Code to execute if
<variable> does not equal the value following any of the cases
break;
}
First we
have an expression or variable, value of that expression or variable is
compared with the values of each case in syntax. If there is match in any of
case only that block of case is executed. Break keyword is used to prevent the
code running into the next case automatically. And if there is no match with
any of the case values then default case is executed just like else statement
in `elseif` condition. Let’s look for an example.
<?php
$x = 20;
switch($x){
case 10:
echo “number is 10”;
break;
case 20:
echo “number is 20”;
break;
case 30:
echo “Number is 30”
break;
default:
echo “unknown number”;
}
?>
If no match
is found from any of the cases then default condition will be executed. It
exactly works like the `elseif` condition. Try to do Grading System in Switch
statement. And further more if you got all the conditional statements try to
make Mark sheet Generator System using if, elseif, or even switch statement.
Thanks Guys.
Any thing missing or any question from this topic you can comment below, I will
be trying to solve and answer your question.
Our Next
topic will be `LOOPS`.
Good Luck J
No comments:
Post a Comment