If, else and elseif
In this tutorial you will learn how to use the following in PHP.
Lets start with if.
PHP Code
<?
$myname = "DanielXP";
if($myname == "DanielXP") {
echo "Your cool!";
}
?>
"$myname" is the variable, I have set this as "DanielXP".
Line 3 is the if line, This gets the variable "$myname" and checks to see if it equals the other variable what i have also set as "DanielXP".
Notice: I have used two = signs in the if. If you don't use two then it would set the variable not check it.
This script would display "You cool!" as "DanielXP" & "DanielXP" are both the same.
Now lets have a look at the else.
PHP Code
<?
$number1 = "1";
$number2 = "2";
if($number1 == $number2) {
echo "Weird, number 1 is the same as number 2!";
}
else
{
echo "Number 1 does not equal number 2.";
}
?>
In this i have set to variables called "$number1" & "$number2" I have set each with a number.
The 4th line of the code is a if like i have explained above it checks to see if 1 equals 2, which it don't so we will move onto the else.
This script will display "Number 1 does not equal number 2.".
Now last of all we can move onto the elseif.
PHP Code
<?
$number1 = "1";
$number2 = "2";
if($number1 == $number2) {
echo "Weird, number 1 is the same as number 2!";
}
elseif($number1 == "1"){
echo "1 is the same as 1!";
}
?>
Same as above it checks the "$number1" & "$number2" variables, As 1 does not equal 2 it will move onto the second part which is the other if. As "$number1" is set as 1 this script will display "1 is the same as 1!".
Thats it for now i hope i have explained this.