如何在heredoc变量中插入php include?

ktm*_*ktm 4 php

我需要在php heredoc变量中包含页面,但它不起作用请帮助我.

$content = <<<EOF
include 'links.php';
EOF;
Run Code Online (Sandbox Code Playgroud)

Sar*_*raz 16

你可以这样做:

ob_start();
include 'links.php';
$include = ob_get_contents();
ob_end_clean();

$content = <<<EOF
{$include}
EOF;
Run Code Online (Sandbox Code Playgroud)

  • 您可以将`ob_get_contents`和`ob_end_clean`组合到`ob_get_clean` :) (3认同)

小智 3

很简单:你做不到。您可以预先包含该文件,将其存储在变量中,然后将其插入到文件中。例如:

$links_contents = file_get_contents('links.php');
//$links_contents = eval($links_contents); // if you need to execute PHP inside of the file
$content = <<<EOF
{$links_contents}
EOF;
Run Code Online (Sandbox Code Playgroud)

  • 这将包括“links.php”的源,而不是执行的内容。 (5认同)