NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > Webmaster Tutorials
Reload this Page PHP: Ternary Operators

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search
0 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 01-14-2007, 10:20 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,565
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship

PHP: Ternary Operators


Note this tutorial was written a while back, as such my coding habits have been changed a bit and some formatting it a bit out of date

This tutorial will explain the method known as "ternary operators" which you will see me especially using a lot. They are also present in javascript and a few other languages, but that's not the point of this tutorial. It's just a quicky and not that in depth but i hope you get something out of it.

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

e.g:

PHP Code:
<?php
if( $arraycount 
{
    
$result 'yes';
????: NamePros.com http://www.namepros.com/webmaster-tutorials/281181-php-ternary-operators.html
}
else
{
    
$result 'no';
}
?>
How stupid is that? So many lines of code for just 1 variable?

The Quick if!

It works like this:

Quote:
function/string ( first condition ) ? "value if true" : "value if false";
So for the above code, we can do this:

PHP Code:
<?php       
$result 
= ( $arraycount ) ? 'yes' 'no';
?>
It does the exact same thing!!

Of course you can also do:
????: NamePros.com http://www.namepros.com/showthread.php?t=281181

PHP Code:
<?php
echo  ( $arraycount ) ? 'yes' 'no';
?>
And another example because im bored and theres nothing on TV.

PHP Code:
echo (eregi('msie'getenv('HTTP_USER_AGENT'))) ? 'Youre using IE!' 'Youre not using IE!'

Anything really, its a good way of keeping order in your code and sparing your fingers. There are a few disadvantages to this technique, for starters it is rarely ever used within a tutorial as the conditional can be missed and since the technique is not widely used it is also not widely understood. The format of the conditional is also not great when debugging.

Any questions/comments, post below and when i get time i or someone else will reply.
Matt.
Matthew. is offline  
Old 01-15-2007, 08:25 AM   #2 (permalink)
NamePros Regular
Join Date: Mar 2006
Location: United Kingdom
Posts: 413
lee101 is a jewel in the roughlee101 is a jewel in the roughlee101 is a jewel in the rough
 




Thanks for explaning that, I'd seen similar syntax a while back and didn't exactly understand how it worked

Rep++ (I think)
__________________
Linux Screenshots
lee101 is offline  
Old 01-15-2007, 08:27 AM   #3 (permalink)
NamePros Regular
Join Date: Feb 2006
Posts: 526
psalzmann is just really nicepsalzmann is just really nicepsalzmann is just really nicepsalzmann is just really nice
 



I use this all the time. Makes code a lot cleaner indeed.
Good call
psalzmann is offline  
Old 01-15-2007, 09:33 AM THREAD STARTER               #4 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,565
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
 


Adoption Breast Cancer Breast Cancer Cancer Survivorship
Thanks for the rep Lee Glad it helped.

Just be sure not to overuse it psalzmann
Matthew. is offline  
Old 01-16-2007, 07:41 AM   #5 (permalink)
New Member
Join Date: Jan 2007
Posts: 6
sathish5566 is an unknown quantity at this point
 



sorrry ya i didnt understand thank you for ur tutorial
sathish5566 is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 12:59 AM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger