我在包含文件中获取变量值时遇到问题.请看下面的我的代码片段
我在test.php中有以下代码
<?php
//this first block includes design.php and takes parameter from url string
ob_start();
require("includes/design.php");
$content = ob_get_contents();
ob_end_clean();
//this second block includes same file but different variable is passed
ob_start();
$itemtest = "This is test";
require("includes/design.php");
$contents_orderslip = ob_get_contents();
ob_end_clean();
?>
Run Code Online (Sandbox Code Playgroud)
和design.php有以下代码
<?php
echo "ITEM TEST = ".$itemtest;
exit;
?>
Run Code Online (Sandbox Code Playgroud)
当我执行test.php时,唯一要打印的是ITEM TEST =.为什么我没有获得价值$itemtest
谢谢
输出design.php保存到变量$contents_orderslip.
你的代码没有回应,添加下面的最后一行:
echo $contents_orderslip;
Run Code Online (Sandbox Code Playgroud)
更新:
由于您编辑了代码并添加exit;到design.php,然后执行将在那里停止,但仍然应该$itemtest定义,因为它在相同的全局范围内.
Update2:
当您发布所有代码时,原因很清楚.如果你有exit;在design.php,代码执行将右后停止exit;,所以你的代码的第二块甚至不运行.