unrelenting Posted March 20, 2009 Share Posted March 20, 2009 I'm a beginner to coding without my if statement but am trying to use these ternary conditional more. I am stumped on getting this to work correctly... <?php echo ' <td style="border-bottom:1px solid black; padding-left:10px;"><div class="' . (in_array($pick3_1, $winners_3)) ? 'team2_div' : 'team2'; . '">' . $pick3_1 , (in_array($pick3_1, $winners_3)) ? '<img style="border: 0; padding-left:10px;" src="images/checkmark.gif" />' : '' . '</div></td>'; ?> The in_array function works fine, it's the ternary part that is causing the foulup. Can someone iron this out for me so I can learn. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/150396-need-assistance-with-a-ternary-conditional/ Share on other sites More sharing options...
Zane Posted March 20, 2009 Share Posted March 20, 2009 use a comma instead of a concat.... (period) before src="images/checkmark.gif" />' : '' . '' after src="images/checkmark.gif" />' : '' , '' Actually...here's the whole thing echo ' ' . $pick3_1 , (in_array($pick3_1, $winners_3)) ? '' : '' , ''; Quote Link to comment https://forums.phpfreaks.com/topic/150396-need-assistance-with-a-ternary-conditional/#findComment-789842 Share on other sites More sharing options...
Mchl Posted March 20, 2009 Share Posted March 20, 2009 Semicolon is unneeded. Also putting parentheses around whole expression helps. .( (in_array($pick3_1, $winners_3)) ? 'team2_div' : 'team2'). Quote Link to comment https://forums.phpfreaks.com/topic/150396-need-assistance-with-a-ternary-conditional/#findComment-789847 Share on other sites More sharing options...
unrelenting Posted March 20, 2009 Author Share Posted March 20, 2009 Cool. Thanks, guys. I guess the biggest error was the unneeded semicolon. Is there a reason to use commas rather than periods? And I will now be using the parenthesis around whole expressions. Makes perfect since. Quote Link to comment https://forums.phpfreaks.com/topic/150396-need-assistance-with-a-ternary-conditional/#findComment-789852 Share on other sites More sharing options...
Zane Posted March 20, 2009 Share Posted March 20, 2009 Is there a reason to use commas rather than periods? use commas when you are going to add a function to your echo statement....e.g. echo "", print_r($rowArray), ""; Quote Link to comment https://forums.phpfreaks.com/topic/150396-need-assistance-with-a-ternary-conditional/#findComment-789868 Share on other sites More sharing options...
redarrow Posted March 20, 2009 Share Posted March 20, 2009 zanus what you mean mate, please give a example sounds good. ok i see your example, you don't even need to use comma it all preference i thort. i do it this way. <?php echo "<pre>"; print_r($rowArray); "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/150396-need-assistance-with-a-ternary-conditional/#findComment-789870 Share on other sites More sharing options...
Mchl Posted March 20, 2009 Share Posted March 20, 2009 If you prefer the code that doesn't work, there's nothing we can do about it I suppose Quote Link to comment https://forums.phpfreaks.com/topic/150396-need-assistance-with-a-ternary-conditional/#findComment-789881 Share on other sites More sharing options...
448191 Posted March 21, 2009 Share Posted March 21, 2009 Is there a reason to use commas rather than periods? use commas when you are going to add a function to your echo statement....e.g. echo "<pre>", print_r($rowArray), "</pre>"; The comma in an echo statement just says "echo this, then this, then this, etc". So the above is functional identical to: echo "<pre>"; echo print_r($rowArray); echo "</pre>"; So you're echoing the result of print_r (which, by default is nothing). Also, print_r without the second parameter set to true has a side effect: it uses output buffering. Using concatenation: echo "<pre>" . echo print_r($rowArray, true) . "</pre>"; That says "assemble a string, then echo it". I have never found any use for passing multiple arguments to echo (using the comma). Concatenation is much easier to read IMO. On a side note, passing multiple arguments to echo is also incredibly slow (relatively, you may never notice it). Quote Link to comment https://forums.phpfreaks.com/topic/150396-need-assistance-with-a-ternary-conditional/#findComment-790288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.