IF STATEMENTS
Basic form
IF (something == something else)
{
THEN Statement
} else {
ELSE Statement
}
If statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.
Another way to extend an if statement is using “elseif”. Similar to “if”, “elseif” requires an expression to evaluate. On the other hand, unlike “else”, “elseif” will execute a statement if the “if” expression evaluates to FALSE, and only if the “elseif” expression evaluates to TRUE.
You can use several “elseif” structures in the same if statement, but remember that an “elseif” statement will only be executed if the previous expressions were all evaluated to “FALSE”, and the current “elseif” expression evaluates to “TRUE”. In other words, once the “if” structure evaluates an expression to “TRUE”, it executes the corresponding statement, then it exits the structure.
Sample code :
if ($num1 > $num2) //is “$num1” bigger than “$num2”?
{
print "The first number is bigger than the second number"; //if it is, do this
$bigger = $num1; //assigning the value of $num1 to $bigger
}
elseif ($num1 == $num2) //the first expression is FALSE, so try this one
{
print “Both numbers are equal”;
}
else //if the previous if/elseif expressions are FALSE, do this...
{
print "The second number is bigger than the first number"; //the only option left
$bigger = $num2; //assigning the value of $num1 to $bigger
}