[advanced search]
Results from the most recent live auction are here.
11 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 01-14-2007, 10:20 AM   · #1
Matthew.
Stud Sausage
 
Location: England
Trader Rating: (25)
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (Donate)
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 > 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:

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 > 1 ) ? 'yes' : 'no';
?>


It does the exact same thing!!

Of course you can also do:

PHP Code:
<?php
echo  ( $arraycount > 1 ) ? '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.


Please register or log-in into NamePros to hide ads
__________________
My NamePros Tools
(firefox plugin, google gadget etc)
Matthew. is offline   Reply With Quote
Old 01-15-2007, 08:25 AM   · #2
lee101
NamePros Regular
 
Name: Lee
Location: United Kingdom
Trader Rating: (8)
Join Date: Mar 2006
Posts: 344
NP$: 2.90 (Donate)
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)
__________________
http://bypasstopsite.com - Submit your proxy!
http://biggertwitter.com - Make twitter a bit bigger!
Currently Developing - Linux Screenshots
lee101 is offline   Reply With Quote
Old 01-15-2007, 08:27 AM   · #3
psalzmann
NamePros Regular
 
Trader Rating: (20)
Join Date: Feb 2006
Posts: 517
NP$: 360.80 (Donate)
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   Reply With Quote
Old 01-15-2007, 09:33 AM   · #4
Matthew.
Stud Sausage
 
Location: England
Trader Rating: (25)
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (Donate)
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
__________________
My NamePros Tools
(firefox plugin, google gadget etc)
Matthew. is offline   Reply With Quote
Old 01-16-2007, 07:41 AM   · #5
sathish5566
New Member
 
Trader Rating: (0)
Join Date: Jan 2007
Posts: 5
NP$: 0.00 (Donate)
sathish5566 is an unknown quantity at this point
sorrry ya i didnt understand thank you for ur tutorial
sathish5566 is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
Traffic Down Under Click to Watch Instant Video YUPPADS
Advertise your business at NamePros
All times are GMT -7. The time now is 09:29 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0