从php变量获取数据

从php变量获取数据,php,variables,foreach,Php,Variables,Foreach,$content=file_get_contents('file.php') echo$内容 不显示任何内容,在浏览器中显示页面源代码时除外。显示如下 <? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?> 它抱怨意外的后果 是否有一种方法可以将文件夹/.php文件内容读取到单个$variable,然后将所有文件夹/.php文件回显/打印到该页面上 已经谢谢你

$content=file_get_contents('file.php')

echo$内容

不显示任何内容,在浏览器中显示页面源代码时除外。显示如下

<? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>
它抱怨意外的后果

是否有一种方法可以将文件夹/.php文件内容读取到单个$variable,然后将所有文件夹/.php文件回显/打印到该页面上


已经谢谢你的帮助了。

这就是你想要做的吗

$content = '';
foreach (glob('folder/*.php') as $class){$content .= file_get_contents($class);}
echo $content;

您正在尝试的不会执行“file.php”的内容,而是在屏幕上显示它们的内容

如果要执行file.php,请使用
eval($content)

要捕获输出,请使用以下方法:

ob_start();              // Don't echo anything but buffer it up

$codeToRun=file_get_contents('file.php'); // Get the contents of file.php
eval ($codeToRun);       // Run the contents of file.php
$content=ob_get_flush(); // Dump anything that should have been echoed to a variable and stop buffering

echo $content;           //echo the stuff that should have been echoed above

你能准确地打印你收到的错误信息吗?实际上是的。。但这两个答案都很好,我可以使用它们。:)谢谢,如果您想执行php代码,DaveyBoy代码会更好。我的不会执行文件中的代码,它只会读取文件内容。因此,如果您需要执行文件,请使用DaveyBoy代码,如果您不需要,我的代码更好(因为您不评估文件)。这是两种不同的用法。
ob_start();              // Don't echo anything but buffer it up

$codeToRun=file_get_contents('file.php'); // Get the contents of file.php
eval ($codeToRun);       // Run the contents of file.php
$content=ob_get_flush(); // Dump anything that should have been echoed to a variable and stop buffering

echo $content;           //echo the stuff that should have been echoed above