Jump to content

Need assistance with a ternary conditional


unrelenting

Recommended Posts

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.

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)) ? '' : '' , '';

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).

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.