Ternary If Else
Posted: Wed Feb 19, 2014 4:44 pm
A Ternary if else is basically a normal if else statement but which you can also use inline.
For example, when you write an if else statement you do it like this:
Well that's it for today i hope you find this tutorial easy to learn and useful.
For example, when you write an if else statement you do it like this:
Code: Select all
Now what happens if you want to write that whole thing in a single line? Well you use it's ternary equivalent, like this:
if(1 + 1 == 2) {
echo "yes";
}
else {
echo "no";
}
Code: Select all
The ternary if else statements work just like the normal ones condition > action if true > action if false only they use this syntax:
echo (1 + 1 == 2) ? "yes" : "no";
Code: Select all
Also they must always be comprised of both (if) and (else) elements and they don't support elseif.Condition ?(if true) do action :(else) do other action
Well that's it for today i hope you find this tutorial easy to learn and useful.