Jump to content

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.