3

I have been stuck trying to write PHP code inside some HTML tags I set up.

I have a variable inside a .php file called $content, and I have a HTML page code inside this variable in the following way:

$content = <<<HTML some html code in here HTML;

Now, within that HTML tag I wanted to add some PHP code, tried using <?php ..... ?>, however I wasn't successful with it. Can anyone help?

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
Jordan
  • 327
  • 8
  • 23
  • [Stackoverflow: Calling PHP functions within HEREDOC strings](http://stackoverflow.com/questions/104516/calling-php-functions-within-heredoc-strings)? OR [Stackoverflow: Use variable within heredoc in PHP](http://stackoverflow.com/questions/11274354/use-variable-within-heredoc-in-php-sql-practice) – Sean Mar 09 '14 at 01:28
  • http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc – Mark Parnell Mar 09 '14 at 01:29

3 Answers3

4

That's a HEREDOC syntax , The closing HTML; should be in margin else it wont work.

Valid Syntax

<?php
$somevar="Hello World";
echo <<<HTML
    <b> Hey this is some text and I am adding this variable $somevar</b>
HTML;

Invalid Syntax

    <?php
    $somevar="Hello World";
    echo <<<HTML
        <b> Hey this is some text and I am adding this variable $somevar</b>
      HTML; //<--- As you can see there is a leading space
Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • I am pretty sure that the tags are on the right place. The issue I am having is to add php code inside those HEREDOC tags (If that is what you call it) – Jordan Mar 09 '14 at 01:41
  • I think you should also wrap your variables quotes `{$somevar}` – Orlo Mar 09 '14 at 01:42
  • It is not just a variable that I want to add, perhaps I wasnt very clear, but I want to add few 5 php lines of code inside those HEREDOC tags – Jordan Mar 09 '14 at 01:48
  • @Jordan, Please don't do that.. doing that so will eventually land yourself in [**Code Smell**](http://en.wikipedia.org/wiki/Code_smell) – Shankar Narayana Damodaran Mar 09 '14 at 01:52
  • @ShankarDamodaran thanks for the opinion, but there is no way to do it, because my << tags, and the tags content I want to fetch from my database using query – Jordan Mar 09 '14 at 01:58
  • @Jordan, Basically you cannot add condition checks , assignment operations done inside of `HEREDOC` syntax. They are just for quoting strings and escaping stuff. You can't write a logic inside of it. – Shankar Narayana Damodaran Mar 09 '14 at 02:04
1

You can always use <script type="text/php">...</script>

sunshinejr
  • 4,834
  • 2
  • 22
  • 32
1

With very old version of PHP, HEREDOC <<<HTML to open a string are not always supported. If you use an old PHP, a workaround is to do:

$myvar="some html code here <a href=\"eee\">link</a>
next line of html code
".$myotherphpvar."
rest of lines"

Note that if you open string with ", you must escape all " for html code with \.

With recent PHP version you can use HEREDOC syntax. This is example of syntax:

    $myvar=<<<HTML
some html code here <a href="eee">link</a>
    next line of html code $myotherphpvar
    rest of lines
HTML;

More examples on page: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

eldy
  • 498
  • 4
  • 7
  • your example is clear when I want to add a variable inside the HEREDOC tags. However, in this occasion I want to add few 5 php lines of code inside those HTML HEREDOC tags, or if I can call a function inside those HEREDOC tags, then things can become easier – Jordan Mar 09 '14 at 01:51