0

For some reason this wont work, i think i have it right because i did read up about this:

echo '<li class="dropdown">';
echo    '<a class="dropdown-toggle" data-close-others="false" data-delay="0" data-hover="dropdown" data-toggle="dropdown" href="#">';
echo        '<img src="<?=$steamprofile['avatar']?>" height="30" style="border-radius: 100%;">';
echo    '</a>';
echo '</li>';

What is my error? i mean i dont really see what is wrong with it i have done it before and it worked?

WillardSolutions
  • 2,316
  • 4
  • 28
  • 38
Toozyne
  • 31
  • 5

3 Answers3

3

Don't use apostrophes (single quotes) when you want to interpolate variables into strings, $ will be literally interpreted as $ not the start of a variable.

Use either double quotes (escaping the double quotes within):

echo "<li class=\"dropdown\">"
    . "<a class=\"dropdown-toggle\" data-close-others=\"false\" data-delay=\"0\" data-hover=\"dropdown\" data-toggle=\"dropdown\" href=\"#\">"
    . "<img src=\"{$steamprofile['avatar']}\" height=\"30\" style=\"border-radius: 100%;\">"
    . "</a>"
    . "</li>";

Or HEREDOC syntax:

echo <<<EOT
<li class="dropdown">
<a class="dropdown-toggle" data-close-others="false" data-delay="0" data-hover="dropdown" data-toggle="dropdown" href="#">
<img src="{$steamprofile['avatar']}" height="30" style="border-radius: 100%;">
</a>
</li>
EOT;
CD001
  • 8,332
  • 3
  • 24
  • 28
  • Actually it's a lot neater that within HTML elements you can use single quotes within the PHP double quoted string. `... data-close-others='false' data-delay='0' ... ` etc. – Martin Apr 19 '16 at 16:05
  • And apostraphes are in this case also often known as *single quotes* :-) – Martin Apr 19 '16 at 16:06
  • Yeah - I know - I just *prefer* to see double quotes in the HTML when I view source :) – CD001 Apr 19 '16 at 16:06
  • hahaha, each to their own. Escaped double quotes in a double quote encased PHP string makes me cross-eyed :-( – Martin Apr 19 '16 at 16:08
  • 1
    in that case `HEREDOC` wins then :) – Martin Apr 19 '16 at 16:09
0

Try this one.

echo '<li class="dropdown">';
echo '<a class="dropdown-toggle" data-close-others="false" data-delay="0" data-hover="dropdown" data-toggle="dropdown" href="#">';
echo '<img src="'.$steamprofile['avatar'].'" height="30" style="border-radius: 100%;">';
echo '</a>';
echo '</li>';
claudios
  • 6,588
  • 8
  • 47
  • 90
0

Sometimes it is easier to simply drop out of php parsing and output straight html. There isn't any measureable performance hit in doing this and often the code is much more readable.

?> //if needed, drop out of PHP parsing

<li class="dropdown">
  <a class="dropdown-toggle" data-close-others="false" data-delay="0" data-hover="dropdown" data-toggle="dropdown" href="#">

Echo only the vars needed within the html.

    <img src="<?= $steamprofile['avatar']; ?>" height="30" style="border-radius: 100%;">
  </a>
</li>

<?php //continue with PHP parsing (if needed)
DFriend
  • 8,869
  • 1
  • 13
  • 26