phpheredocnowdoc

How can I write PHP syntax in a string


I tried to write some content in order to write in a php file and in my $content variable i use Heredoc and nowdoc but php tags seems not working and are still shown.

$content = <<< EOT
<?php
    include $_SERVER['DOCUMENT_ROOT'] . '/home/index2.php';
?> 
EOT;

Any ideas ?


Solution

  • Try using Output buffering instead of a heredoc:

    ob_start();
    include $_SERVER['DOCUMENT_ROOT'] . '/home/index2.php';
    $content = ob_get_clean();
    

    Reference: