PHP Guide 4: Conditionals and Operators
A guide to using conditionals in PHP: if, else, elseif.
Index of Contents
»Comparison Oerators
»Conditionals
»Logical Operators
Comparison Operators
Before we start using conditionals, we need to go over comparison operators really quick. A comparison operator compares one value to another. You are probably familiar with these: equal to, greater than, less than, is no equal to, greater than or equal to, less than or equal to. Here is how we use this in PHP.
// == Equal to
// != Not equal to
// > Greater than
// < Less than
// >= Greater than or equal to
// <= Less than or equal to
// We will be using these shortly for our conditional statements.
?>
Conditionals
Conditionals are used to check if a condition is true or false, and then execute some code based on that. "if" is used to initiate a conditional statement. Here is an example
if($value == 3)
{
echo 'Value is 3!';
}
// Above we used a conditional to check if $value equals 3. And if the result is true it prints: Value is 3!
// Notice that the syntax is similar to loops. No semi-colon after the if statement, and the block to be
// executed is inside the curly brackets. Here is another example:
$this = 4
if($variable > $this)
{
echo $variable . ' is greater than ' . $this;
}
// Often times we want to execute some code if the conditional statement is false, and for that we use "else":
$this = 4
if($variable > $this)
{
echo $variable . ' is greater than ' . $this;
}
else
{
echo $variable . ' is not greater than ' . $this;
}
// "else" is used just as "if" is, except it does not take any arguments. It is simply a catch-all should the
// comparison in the if statement be false. We can use "elseif" in combination with the above to look for
// any number of conditions:
$this = 4
if($variable > $this)
{
echo $variable . ' is greater than ' . $this;
}
elseif($variable == $this)
{
echo $variable . ' is equal to ' . $this;
}
else
{
echo $variable . ' is less than ' . $this;
}
// Notice that "elseif" DOES take an argument just like the "if" statement did. PHP checks first to see if the "if"
// statement is true, then if it is not it checks the "elseif" statement. Finally if that is not true it runs the
// "else" statement, which is a catch-all if the above statements are all false.
?>
Logical Operators
There is one more important concept to learn about conditionals. An "if" statement can check more than one condition. You can check if one condition is true and another condition is true. You can also check if one condition is true or another condition is true. For this we use "&&", "||", and "!".
// Let's say we want to check if two conditions are both met, and then run some code:
if($var == 5 && $this == 3)
{
echo 'Some code';
}
// PHP will only echo "Some code" if both conditions are met. We can also check to see if EITHER condition is met
// using ||:
if($var == 5 || $this == 3)
{
echo 'Some code';
}
// echo "Some code" will run in this case if $var equals 5 even if $this does not equal 3. Lastly we have the
// not operator, represented by the exclamation mark.
if(!($var == 5))
{
echo 'Some code';
}
// In the above example, the echo will only execute if $var does not equal 5. Think of using the ! as a way to
// reverse the statement, so that the code only executes if the opposite is true. In this case the same result
// could have been achieved with "$var != 5" if desired.
?>