Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP tag inside <<<HTML ...... HTML; [duplicate]

Tags:

html

php

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?

like image 206
Jordan Avatar asked Sep 20 '26 23:09

Jordan


2 Answers

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
like image 89
Shankar Narayana Damodaran Avatar answered Sep 22 '26 12:09

Shankar Narayana Damodaran


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

like image 23
sunshinejr Avatar answered Sep 22 '26 12:09

sunshinejr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!