TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-29-2009, 06:02 AM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default Nested Ternary = Fail

Hmm. It's times like this that the coffee isn't fixing my problem, so I'm going to post in Absolute Beginners.

Can somebody spot the error in this nested ternary that I'm missing (and don't say the error is using a ternary, I'll poke you in the eye)? The preceding operation is there as a test and produces the desired result. The nested version is producing a fail. Fail!

php Code:
echo isset( $one[$i]['timestamp'] ) ? $one[$i]['timestamp'] : 'fail';
echo ' :: ';

$timestamp = isset( $one[$i]['timestamp'] )   ? $one[$i]['timestamp'] :
             isset( $two[$i]['timestamp'] )   ? $two[$i]['timestamp'] :
             isset( $three[$i]['timestamp'] ) ? $three[$i]['timestamp'] :
                                                    'fail';
die( $timestamp );
delayedinsanity is offline  
Reply With Quote
Old 10-29-2009, 11:52 AM   #2 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

You need to enclose each level

PHP Code:
isset( $one[$i]['timestamp'] )  ? $one[$i]['timestamp'] : ( isset( $two[$i]['timestamp'] )   ? $two[$i]['timestamp'] : ( isset( $three[$i]['timestamp'] ) ? $three[$i]['timestamp'] : 'fail')); 
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
The Following User Says Thank You to ioan1k For This Useful Post:
delayedinsanity (10-29-2009)
Old 10-29-2009, 04:34 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Ah. Doesn't quite look as pretty at that point. S'too bad. (That's what I get for reading a post on stack overflow as php specific that was actually language agnostic)
delayedinsanity is offline  
Reply With Quote
Old 11-01-2009, 08:57 PM   #4 (permalink)
The Wanderer
 
Join Date: Aug 2009
Posts: 17
Thanks: 0
Rhinos is on a distinguished road
Default

It doesn't look that bad with the parenthesis. Also I think indenting each ternary statement may improve readability also.


PHP Code:
$timestamp = (isset( $one[$i]['timestamp'] )   ? $one[$i]['timestamp'] :
               (isset( 
$two[$i]['timestamp'] )   ? $two[$i]['timestamp'] :
                 (isset( 
$three[$i]['timestamp'] ) ? $three[$i]['timestamp'] :
                   
'fail'
             
))); 
Rhinos is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested while loop needed for this? gtifllm General 1 08-17-2009 07:50 PM
Nested While Loop Issue Jako General 3 08-06-2008 06:13 AM
having nested mysql_fetch_assoc statements sarmenhb Absolute Beginners 5 06-13-2008 04:12 PM
Need help on nested loop! pabin Absolute Beginners 4 05-12-2008 10:27 AM


All times are GMT. The time now is 10:35 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design