标签:php
我试图将包含文件的内容添加到变量.
例如,我有一个名为$form的变量:
$form = 'Hello world';
$form .= include('sidebar.inc.php');
还有另一种方法吗?因为上面提到的方法给我带来了一些问题……
解决方法:
您可以使用file_get_contents()将其存储在变量中,而不是使用include():
$form = file_get_contents('sidebar.inc.php');
这不会在将PHP存储到变量中之前对其进行解析,因此另一个选项是从include手册中发布的here解决方案:
$string = get_include_contents('sidebar.inc.php');
function get_include_contents($filename) {
if (is_file($filename)) {
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
标签:php
来源: https://codeday.me/bug/20190723/1512994.html