PHP Basic
Introduction to PHP PHP Starting Requirements Downloading XAMPP Local Server PHP Variable Concept and Data Type PHP Coding Syntax Showing Output in PHP PHP String Concatenation Increment & Decrement in PHP PHP Logical Operator and Usage Part-1 PHP Logical Operator and Usage Part-2 PHP Comparison Operators Arithmetic and Assignment Operators PHP Switch Case Statement For loop in PHP Super Global Variable $_REQUEST and $_GET Part-1 String Length in PHP Function Declaration and UsageIn part 1 we have discussed about logic and var_dump() function. In this part we will discuss about usage of var_dump() function, if, else, else if and examples. We have used $personOne and $personTwo in last part 1 where we used 35 for $personOne and 40 for $personTwo. Let's compare and find out the boolean output from var_dump().
<!--?php
$personOne = 35;
$personTwo = 40;
var_dump($personOne --> $personTwo); //condition is false
?>
Output:
bool(false)
We used greater(>) sign, so var_dump() will give false. Because $personOne is not bigger than $personTwo. But if we use less(<) than sign then var_dump() give true.
<!--?php
$personOne = 35;
$personTwo = 40;
var_dump($personOne < $personTwo); //condition is true
?-->
bool(true)
Now condition is true. For this reason var_dump() returns true. You can check any kind of variable type or condition using this simple var_dump() function. Let's see some real life example of if, else and elseif
<!--?php
$personOne = 35;
$personTwo = 40;
if ($personOne < $personTwo) {
echo "Person is smaller";
}
?-->
Output:
Person is smaller
<!--?php
$firstNum = 10;
$secondNum = 20;
if ($firstNum!=$secondNum) {
echo "Two numbers are not same";
}
?-->
Output:
Two numbers are not same
<!--?php
$firstNum = 20;
$secondNum = 20;
if ($firstNum==$secondNum) {
echo "Two numbers same";
}else{
echo "Two numbers are not same";
}
?-->
Two numbers same
<!--?php
$marks = 45;
if ($marks -->= 80) {
echo "You have got GPA 4.00";
} else if ($marks >= 75) {
echo "You have got GPA 3.75";
} elseif ($marks >= 70) {
echo "You have got GPA 3.50";
} elseif ($marks >= 65) {
echo "You have got GPA 3.25";
} elseif ($marks >= 60) {
echo "You have got GPA 3";
} else if ($marks >= 55) {
echo "You have got GPA 2.75";
} else if ($marks >= 50) {
echo "You have got GPA 2.50";
} else if ($marks >= 45) {
echo "You have got GPA 2.25";
} else if ($marks >= 40) {
echo "You have go GPA 2.00";
} else {
echo "You have failed";
}
?>
Output
You have got GPA 3
Hope that you have got a better ideas about conditional logic. This page will be modified also in future with several examples and explanation.
If you think you have query... then For contacting with us use Facebook . You can also use Github