Help - Search - Members - Calendar
Full Version: Ternary operators (The Quick IF)
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Matthew.
Another tutorial written by me a while back, enjoy victory.gif

Ever found your self diving into endless reams of code just to set a simple variable??

e.g:

CODE
<?php

if( $arraycount > 1 )
{
    $result = 'yes';
}
else
{
    $result = 'no';
}
?>


How stupid is that? So many lines of code for just 1 variable?

The Quick if!

It works like this:

function/string ( first condition ) ? "value if true" : "value if false";

So for the above code, we can do this:

CODE
<?php      
$result = ( $arraycount > 1 ) ? "yes" : "no";
?>


It does the exact same thing!!

Of course you can also do:

CODE
<?php
echo  ( $arraycount > 1 ) ? "yes" : "no";
?>



Remember, you can EVEN do this:
CODE
echo 'Is arraycount greater than 1? '. ( $arraycount > 1 ) ? "yes" : "no";


And another example cuz im bored and theres nothing on TV.

CODE
echo (eregi("MSIE",getenv("HTTP_USER_AGENT"))) ? 'Your using IE!' : 'Your not using IE!';



Anything really, its a good way of keeping order in your code, sparing your fingers, and loading pages better!
Indigo
Thank you! I can use this for quite many things smile.gif
Great that you added a lot of examples too.

Though the other way is a lot easier to read, at least too me.
Matthew.
Ah, once ya use this a few times its simple as pie bigwink.gif
Futingkiller
this :"echo (eregi("MSIE",getenv("HTTP_USER_AGENT"))) ? 'Your using IE!' : 'Your not using IE!';"
can be found in javascript by:
document.write ((document.all)?'Your using IE!' : 'Your not using IE!');
Matthew.
hehe yes i know, i wrote a tutorial about document.all a while back bigwink.gif
Indigo
Just so you know it, .Matt - I love it now! victory.gif
Matthew.
lol, thats quite a random update indigo but goodo victory.gif

Glad it helped someone smile.gif
cheerio
It's pretty funny, because you don't see the most basic, useful things like this at first.
Hit3k
This has to be one of the most uncommon if else statements i've ever seen
Its still simple though Great tut!
Jem
There's a reason why most tutorial writers don't use this method - because it's crap. It's incredibly inflexible: when updating code it's easily skimmed over because of how hard it is to read at quick pace, and if you want to add to the if statement you have to rework lines of code.

It's considered bad practise to use it in public code.
rc69
True, it is easy to miss, but it is also really convenient to use. I can't say which is best practice, but i do perfer to use this method when ever i can (and when i remember to).

p.s. php.net's say on the subject. smile.gif http://php.net/manual/en/language.operator...parison.ternary
Hayden
I really liked this when I first saw it Matt. smile.gif


I've enjoyed playing with it and applying it. I even started applying it to some javascripts. tongue.gif


Curious though, am I understanding it correctly that you have to have an IF and ELSE when using the ternary operators?
Arutha
Yea this is simply another way you can notice the similarities between the formatting off Javascript and PHP, thanks for posting about this smile.gif.
Indigo
How do I use elseif with ternary operators?
Mr. Matt
I don't think you can Indigo. Have to use the long method for those.
SecondV
actually, you can do elseif
CODE
<?php

echo ($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ($_ENV['REMOTE_ADDR'] ? $_ENV['REMOTE_ADDR'] : '');

?>


Which would be the same as
CODE
<?php

if ($_SERVER['REMOTE_ADDR'])
{
    echo $_SERVER['REMOTE_ADDR'];
}
else if ($_ENV['REMOTE_ADDR'])
{
    echo $_ENV['REMOTE_ADDR'];
}
else
{
    echo '';
}

?>


bigwink.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.