Jump to content

PHP inside html


Andrew R

Recommended Posts

Can anybody tell me why this script below isn't working.  As you can see if page = names it will echo that.  The problem is inside the echo I have some php.  I tried putting the php in "" but it still comes up with an error.

[code]    <?

if ($page == 'names'){


echo"</td>
  </tr>
  <tr class=\"navtext2\">
    <td height=\"22\" colspan=\"3\" valign=\"top\"> <div id=\"navcontainer\">
       
      <div align=\"left\"><font color=\"#747474\">" echo $row_names['fullname']; "</font></div>
      </div>
      <div>
        <div align=\"left\"></div>
      </div>
      <div></div></td>
  </tr>";



}
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/28995-php-inside-html/
Share on other sites

When you want to echo variables inside html, do not use echo use the period (.) before and after the variable.
[code]
<?

if ($page == 'names'){


echo"</td>
  </tr>
  <tr class=\"navtext2\">
    <td height=\"22\" colspan=\"3\" valign=\"top\"> <div id=\"navcontainer\">
       
      <div align=\"left\"><font color=\"#747474\">" .$row_names['fullname']."</font></div>
      </div>
      <div>
        <div align=\"left\"></div>
      </div>
      <div></div></td>
  </tr>";



}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/28995-php-inside-html/#findComment-132845
Share on other sites

[quote]When you want to echo variables inside html, do not use echo use the period (.) before and after the variable.[/quote]
Its ok for him, he is using double quotes, which you can echo out vars without having to use the period

The problem is probably this part though: echo $row_names['fullname'];

its already in an echo, you don't need to add another one, so change it to this:
[CODE]
    <?php

if ($page == 'names'){


echo"</td>
  </tr>
  <tr class=\"navtext2\">
    <td height=\"22\" colspan=\"3\" valign=\"top\"> <div id=\"navcontainer\">
       
      <div align=\"left\"><font color=\"#747474\"> {$row_names['fullname']} </font></div>
      </div>
      <div>
        <div align=\"left\"></div>
      </div>
      <div></div></td>
  </tr>";



}
?>[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/28995-php-inside-html/#findComment-132848
Share on other sites

A better option is use the HEREDOC syntax, no need to escape any quotes:
[code=php:0]<?php

if ($page == 'names'){


echo <<<HTML_CODE
</td>
  </tr>
  <tr class="navtext2">
    <td height="22" colspan="3" valign="top"> <div id="navcontainer">
       
      <div align="left"><font color="#747474"> {$row_names['fullname']} </font></div>
      </div>
      <div>
        <div align="left"></div>
      </div>
      <div></div></td>
  </tr>
HTML_CODE;
// DO NOT INDENT OR PUT ANYTHING ON THE LINE ABOVE

}
?>[/code]

When using HEREDOC syntax you may need to wrap curly braces around your variable names ({$my_var}), especially if its an array.
Link to comment
https://forums.phpfreaks.com/topic/28995-php-inside-html/#findComment-132865
Share on other sites

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.