Quick tutorial: php if and else
The if construct is an important feature of many programming languages, allowing for particular code to be excuted depending on certain conditions.
The basic way of using the if construct is:
if (condition) {
do this if the condition is satisfied or is true;
}
You can include an else construct to execute different code if the condition is not met. This is typically used in this way:
if (condition) {
do this if the condition is satisfied or is true;
} else {
do this if the condition is not satisfied or is false;
}
By not stating an else construct, php will “do nothing” by default when the condition is not satisfied.
If the code to be executed is short, you can write your if and else constructs using the shorthand. For example:
(condition) ? do this if condition true : do this if condition is false;
Real examples
A real example in shorthand:
($variable) ? echo '$variable is set' : echo '$variable is not set';
The above example is the same as:
if ($variable) {
echo '$variable is set';
} else {
echo '$variable is not set';
}
The example is stating if $variable is set, then print ‘$variable is set’, otherwise print ‘$variable is not set’.
More information:

