In PHP, there are a couple ways of writing an if-else statement. One is:
if($variable == 'something'){
$result = 'the variable equals something';
}else{
$result = 'the variable does not equal something';
}
echo $result
Another way of writing the above if-else statement is:
if ($variable == 'something') : $result = 'the variable equals something'; else: $result = 'the variable does not equal something'; endif; echo $result
I used the first of the above syntax for a long time (the second I never really cared for).. but couple months ago, I started paying more attention to a completely different if-else statement syntax, used particularly for assigning values to a single variable based on the met conditions in the if-else statement. Here’s the example:
$result = $variable == 'something' ? 'the variable equals something' : 'the variable does not equal something'; echo $result;
What I like about this alternative way of writing an if-else statement is not only less code, but cleaner code as well.
You can also nest the if-else statements like this:
$result = $test1 ? $test2 ? 'test1 true and test2 true' : 'test1 true and test2 false' : 'test1 false';
echo $result;