Jump to content


Photo
- - - - -

Ternary operators (The Quick IF)


  • Please log in to reply
15 replies to this topic

#1 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 14 May 2006 - 03:31 PM

Another tutorial written by me a while back, enjoy ;)

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

e.g:

<?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:

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

It does the exact same thing!!

Of course you can also do:

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


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

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

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!

Edited by Matthew., 22 April 2007 - 07:39 AM.


#2 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 15 May 2006 - 06:08 AM

Thank you! I can use this for quite many things ;)
Great that you added a lot of examples too.

Though the other way is a lot easier to read, at least too me.

Edited by Indigo, 15 May 2006 - 06:20 AM.


#3 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 15 May 2006 - 11:47 AM

Ah, once ya use this a few times its simple as pie :blink:

#4 Futingkiller

Futingkiller

    Young Padawan

  • Members
  • Pip
  • 110 posts

Posted 15 May 2006 - 02:02 PM

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!');

#5 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 15 May 2006 - 03:16 PM

hehe yes i know, i wrote a tutorial about document.all a while back :D

#6 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 04 August 2006 - 04:30 PM

Just so you know it, .Matt - I love it now! :P

#7 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 04 August 2006 - 04:55 PM

lol, thats quite a random update indigo but goodo :)

Glad it helped someone :huh:

#8 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 04 August 2006 - 05:15 PM

It's pretty funny, because you don't see the most basic, useful things like this at first.

#9 Hit3k

Hit3k

    Young Padawan

  • Members
  • Pip
  • 120 posts
  • Gender:Male
  • Location:Australia

Posted 06 August 2006 - 10:15 AM

This has to be one of the most uncommon if else statements i've ever seen
Its still simple though Great tut!

#10 Jem

Jem

    Young Padawan

  • Members
  • Pip
  • 93 posts
  • Location:England
  • Interests:Photography, design &amp; developing, walking, cycling, reading.

Posted 06 August 2006 - 11:25 AM

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.

#11 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 12 August 2006 - 01:38 AM

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. :friends: http://php.net/manua...parison.ternary

#12 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 18 February 2007 - 03:18 AM

I really liked this when I first saw it Matt. :D


I've enjoyed playing with it and applying it. I even started applying it to some javascripts. ;)


Curious though, am I understanding it correctly that you have to have an IF and ELSE when using the ternary operators?

#13 Arutha

Arutha

    Young Padawan

  • Members
  • Pip
  • 144 posts
  • Gender:Male
  • Location:Southampton, England

Posted 19 February 2007 - 04:47 PM

Yea this is simply another way you can notice the similarities between the formatting off Javascript and PHP, thanks for posting about this ;).

#14 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 22 April 2007 - 07:19 AM

How do I use elseif with ternary operators?

#15 Mr. Matt

Mr. Matt

    Moderator

  • Validating
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 22 April 2007 - 07:40 AM

I don't think you can Indigo. Have to use the long method for those.

#16 SecondV

SecondV

    Young Padawan

  • Members
  • Pip
  • 28 posts
  • Gender:Male
  • Location:Kentucky
  • Interests:All things PHP &amp; MySQL :)

Posted 27 April 2007 - 03:23 AM

actually, you can do elseif
<?php

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

?>

Which would be the same as
<?php

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

?>

:lol:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users