The community is working on translating this tutorial into Portuguese, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
More if statements
In this chapter, we will look at more ways of using the if statement and its related operators and structures. Once you dig deeper into PHP, you will soon realize that it's a language with many alternative ways of doing various things.
First of all, it's important to know how to treat an if statement with more than one condition. If statements can be nested, like this:
<?php
$number = 10;
if($number > 0)
if($number < 20)
echo "Number is bigger than 0 but smaller than 20!";
?>
The echo line will only be executed if both if statements evaluates to true. However, the example could have been written like this as well:
<?php
$number = 10;
if(($number > 0) and ($number < 20))
echo "Number is bigger than 0 but smaller than 20!";
?>
How you should write it depends on the situation and your personal preferences.
In the previous chapter, we looked at the else statement. PHP has another flavor of this, the elseif statement. That's right, it's an if and an else statement in one. It can be used like this:
<?php
$number = 10;
if($number > 20)
echo "Number is bigger than 20!";
elseif($number > 10)
echo "Number is bigger than 10!";
else
echo "The number seems a bit low..."
?>
This is nothing but an alternative syntax. You may as well have written an else statement, followed by yet another if statement.
Instead of using regular PHP blocks, surrounded with curly brackets, PHP supports an alternative syntax for its control structures like if, while etc. It's not very commonly used, but here's an example anyway:
<?php
$number = 10;
if($number == 10):
echo "Number is 10!";
else:
echo "Number is not 10.. ";
echo "But why?";
endif;
?>
If you like this syntax better, you can use it - it doesn't make much of a difference.
The ternary operator
Sometimes, pulling out an entire if and else statement feels like a bit much. For instance, have a look at this example, where we use an if statement to append the proper word to an output variable, based on whether or not there are any items in our basket:
<?php
$numberOfItems = 2;
$output = "There is ";
if($numberOfItems > 0)
$output .= "something";
else
$output .= "nothing";
$output .= " in your basket";
echo $output;
?>
Using the ternary PHP operator, we could write this way shorter:
<?php
$numberOfItems = 0;
$output = "There is " . (($numberOfItems > 0) ? "something" : "nothing") . " in your basket";
echo $output;
?>
This really brought down the number of code lines. The ternary operator is like a short version of an if-and-else statement, all in one line. A condition inside a set of parentheses is the if statement. This is followed by a question mark, and after that, the result if the condition is evaluated to true. Then we have a colon, followed by the result if the condition is evaluated to false. To have all this inside the assignment line, we wrap it all inside a set of parentheses, to tell PHP that this is actually one statement inside the line.
Whether you use this alternative syntax is once again up to you. In some cases it might make sense because you're doing something really simple, while you may wish to stick to the long way of writing it in other cases, to make your code more readable.