Ternary If Else
5 posts
Page 1 of 1
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

Last edited by XTechVB on Wed Feb 19, 2014 5:48 pm, edited 4 times in total.
You can find me on Facebook or on Skype mihai_92b
Interesting I didn't know that.
However you still can do this:
However you still can do this:
Code: Select all
if(1 + 1 == 2) { echo "yes"; } else { echo "no"; }
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
smashapps wrote:Interesting I didn't know that.True but i prefer Ternary when i need to check something inline, plus is shorter
However you still can do this:
Code: Select allif(1 + 1 == 2) { echo "yes"; } else { echo "no"; }

You can find me on Facebook or on Skype mihai_92b
XTechVB wrote: True but i prefer Ternary when i need to check something inline, plus is shorterAnd looks neater, of course..

5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023