[Tutorial] PHP's Ternary Conditional Operator

Example of PHP code.

Moderator: Moderators

[Tutorial] PHP's Ternary Conditional Operator

Postby pentapenguin on Wed May 11, 2005 5:24 pm

Hello.
This short tutorial will introduce you to PHP's ternary conditional operator. Whether you've been programming for a long time or just getting started, the ternary operator is a very handy trick to know. However, many programmers don't use this because it looks so confusing. But hopefully this tutorial will help clear up any confusion.

What exactly is a ternary conditional operator?
Basically, it's a very shortened form of an if/else statement.
Let's look at an example if/else that could be turned into a ternary form:
Code: Select all
<?php
$my_color = 'green';

if ($my_color == 'green')
{
   print 'My favorite color is green.';
}

else
{
   print 'My favorite color is ' . $my_color;
}
?>


All of that could be succinctly rewritten as:
Code: Select all
<?php
$my_color = 'green';
( $my_color == 'green' ) ? print 'My favorite color is green.' : print 'My favorite color is ' . $my_color;
?>


The basic form of the ternary operator is this:
Code: Select all
( condition ) ? true_part : false_part

The condition is the same condition you'd use in a if() statement; the true_part is the what you'd put to execute is an if() is true; and the false_part is what you'd put in the else part of a statement.

One other thing we'll look at.
The ternary operator can also assign variables too.
Look at this code:
Code: Select all
<?php
$my_color = 'green';
$message = '';

if ($my_color == 'green')
{
   $message = 'My favorite color is green.';
}

else
{
   $message = 'My favorite color is ' . $my_color;
}
?>


That could also be rewritten as:
Code: Select all
<?php
$my_color = 'green';
$message = ($my_color == 'green') ? 'My favorite color is green.' : 'My favorite color is ' . $my_color;
print $message;
?>


The ternary operator is ideal for shorter if/else statements like the examples above and will save you typing out longer statements. Also using it will make your code cleaner and more readable which should be the goal of all programmers.
User avatar
pentapenguin
Moderator
Moderator
 
Posts: 1466
Joined: Tue Sep 14, 2004 4:15 pm
Location: Georgia, USA

Postby Phantom on Wed May 11, 2005 6:47 pm

Wow, very well done. While looking through the phpBB coding, I was wondering what those were. Thanks a bunch!
"Best of the best of the best, sir! With honors!" -- MIB
[ jphantom.com ]
Phantom
Registered User
Registered User
 
Posts: 245
Joined: Mon Apr 18, 2005 3:24 am

Postby pentapenguin on Wed May 11, 2005 8:39 pm

Glad you liked it. :)
I never realized how handy it is until I started dabbling with phpBB.
User avatar
pentapenguin
Moderator
Moderator
 
Posts: 1466
Joined: Tue Sep 14, 2004 4:15 pm
Location: Georgia, USA

Postby Joe Belmaati on Tue May 31, 2005 3:37 pm

I've been struggling with elseif's and the ternary operator (which I have come to love). Is it possible to use the ternary operator for this....?
User avatar
Joe Belmaati
Registered User
Registered User
 
Posts: 148
Joined: Tue Sep 07, 2004 9:26 pm
Location: Copenhagen, Denmark

Postby kvfb97 on Tue May 31, 2005 6:15 pm

Just learnt something new, which is alot handier than IF. Thanks!
Tip: Don't argue with idiots, they'll drag you down to their level and beat you with experience.
User avatar
kvfb97
Registered User
Registered User
 
Posts: 1774
Joined: Mon Aug 02, 2004 8:18 am
Location: Northern Ireland, UK

Postby UseLess on Tue May 31, 2005 8:46 pm

Greetings,

[quote user="Joe Belmaati" post="37426"]I've been struggling with elseif's and the ternary operator (which I have come to love). Is it possible to use the ternary operator for this....?[/quote]

Yes it is, but it can then get very complicated very quickly... you basically add another condition in the false part like so;

Code: Select all
$colour = ( ( $choice == 'blue' ) ? 'blue' : ( ($choice == 'red') ? 'red' : 'cannot decide') );


which is equivalent to;

Code: Select all
if( $choice == 'blue' )
{
$colour = 'blue';
}
else if( $choice == 'red' )
{
$colour = 'red';
}
else
{
$colour = 'cannot decide';
}
Last edited by UseLess on Wed Jun 01, 2005 11:26 am, edited 1 time in total.
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby kvfb97 on Tue May 31, 2005 9:48 pm

Think the ternary one is handy, but if your using elseif's the normal one would be better...
Tip: Don't argue with idiots, they'll drag you down to their level and beat you with experience.
User avatar
kvfb97
Registered User
Registered User
 
Posts: 1774
Joined: Mon Aug 02, 2004 8:18 am
Location: Northern Ireland, UK

Postby DooBDee on Tue May 31, 2005 9:49 pm

[quote user="kvfb97" post="37467"]Think the ternary one is handy, but if your using elseif's the normal one would be better...[/quote]

I agree.
DooBDee
Website Ninja
DooBDee.net
User avatar
DooBDee
Moderator
Moderator
 
Posts: 3530
Joined: Tue Mar 01, 2005 6:28 pm
Location: DooBDee.net

Postby Joe Belmaati on Tue May 31, 2005 10:04 pm

Thank you :D
User avatar
Joe Belmaati
Registered User
Registered User
 
Posts: 148
Joined: Tue Sep 07, 2004 9:26 pm
Location: Copenhagen, Denmark

Re: [Tutorial] PHP's Ternary Conditional Operator

Postby pentapenguin on Wed Jun 01, 2005 4:42 am

[quote user="pentapenguin" post="33230"]The ternary operator is ideal for shorter if/else statements like the examples above[/quote]
I've seen some people who have crammed it seemed like a dozen ifs/elses in a ternary statment and let me tell ya...it ain't pretty! :lol:
User avatar
pentapenguin
Moderator
Moderator
 
Posts: 1466
Joined: Tue Sep 14, 2004 4:15 pm
Location: Georgia, USA

Postby DooBDee on Wed Jun 01, 2005 10:27 am

amazing! lol

Dont suppose you have an example of it ? :p
DooBDee
Website Ninja
DooBDee.net
User avatar
DooBDee
Moderator
Moderator
 
Posts: 3530
Joined: Tue Mar 01, 2005 6:28 pm
Location: DooBDee.net

Postby UseLess on Wed Jun 01, 2005 11:28 am

Greetings,

If you want to see a complicated use of this then look in 'card.php' as supplied with the Yellow Card mod, it's at the top of the file... and it's LONG!
Movie Quote:
It's not the years honey, it's the mileage...

I do not provide any install services for phpBB, Mods or Styles.
Please do not pm me for support/scripting help - you won't get any reply. If you have a question then make a post in the appropriate forum.
User avatar
UseLess
Registered User
Registered User
 
Posts: 6220
Joined: Mon Sep 27, 2004 2:14 am
Location: North East, UK

Postby ewelin on Tue Jun 07, 2005 8:56 pm

extremely useful.... thanks for the straight forward explaination
ewelin
Registered User
Registered User
 
Posts: 679
Joined: Thu Apr 14, 2005 8:16 pm


Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests