从php变量获取数据

时间:2012-04-03 15:31:27

标签: php variables foreach

$ content = file_get_contents('file.php');

echo $ content;

没有任何显示,期望在浏览器中显示页面源代码时显示为

<? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>

因此在获取内容时不会执行脚本..

file.php包含此代码 <? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>

如果我做下一个

$content = foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>

它抱怨意外的预告......

有没有办法将文件夹/ .php文件内容读取到单个$ variable,然后回显/打印所有文件夹/ .php文件到它应该的页面?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

这就是你想要做的吗?

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

答案 1 :(得分:1)

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

如果要执行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