添加第6个变量时,PHP变量在heredoc中不起作用
我在三周前写了一个运行良好的herdoc,直到今天它还使用了五个PHP变量 以下是报告:添加第6个变量时,PHP变量在heredoc中不起作用,php,html,heredoc,Php,Html,Heredoc,我在三周前写了一个运行良好的herdoc,直到今天它还使用了五个PHP变量 以下是报告: echo <<<itemDetails_HEREDOC <img src="$sitePath/images/logo-landing2.png" /> <form action="$thisFilename" method="post" name="arti-form" id="arti-formId"> <label styl
echo <<<itemDetails_HEREDOC
<img src="$sitePath/images/logo-landing2.png" />
<form action="$thisFilename" method="post" name="arti-form" id="arti-formId">
<label style="display: inline-block; width: 140px">OWNER NAME:</label><input type="text" style="width: 300px"
id="ownerId" name="ownerName" readonly="readonly" value="$theLoggedInUser" /><br />
<label style="display: inline-block; width: 140px">THE ITEM:</label><input type="text" style="width: 300px"
readonly="readonly" id="itemNameId" name="itemName" value="$itemName" /><br />
<label style="display: inline-block; width: 140px">LINK:</label><input type="text" style="width: 300px"
readonly="readonly" id="itemLinkId" name="link" value="$theLinkID" /><br />
<label style="display: inline-block; width: 140px">ERA:</label><input type="text" style="width: 70px"
readonly="readonly" id="itemEraId" name="itemEra" value="$itemEra" /><br />
itemDetails_HEREDOC;
以下是五个PHP变量,它们已经在heredoc中运行了三周,并且工作正常——在HTML发送到我的浏览器之前,它们被正确地替换为它们的值:
- $thisFilename
- $theLoggedInUser
- $itemName
- $theLinkID
- $itemEra
$sitePath = "http://localhost/Arti-facks";
在showItem.php的顶部,在过去的三周里,我有一个声明,其中包括拉入globals.php:
require_once 'globals.php'; // Variables and statics used throughout
所以今天我在globals.php中添加了$sitePath声明(如上)
为了证明showItem.php能够“看到”我添加到globals.php的新声明的$sitePath,我回显了变量——果然,showItem.php能够100%看到我新声明的$sitePath
然而——尽管如此——尽管三个星期来使用了上述herdoc和其他五个PHP变量--
heredoc未获取$sitePath
当我“查看页面源代码”时,我看到上面的herdoc的$sitePath行:
<img src="/images/logo-landing2.png" />
您可以看到/images/logo-landing2.png之前的$sitePath部分为空或缺失
为什么在这三个星期里,我在上面的herdoc中成功地解决了五个PHP变量,但今天我添加了第六个PHP变量,就好像
“天哪,您在heredoc中的PHP变量配额最大为5。现在我们看到您正在尝试使用第6个PHP变量——您在想什么呢?”
由于在函数内调用了herdeoc,因此全局变量
$sitePath
不在作用域内。在函数顶部,使用global
关键字:
function yourfunction() {
global $sitePath;
// Then build the HEREDOC
}
或者使用herdoc中的$GLOBALS
数组:
echo <<<itemDetails_HEREDOC
<img src="{$GLOBALS['sitePath']}/images/logo-landing2.png" />
<form action="$thisFilename" method="post" name="arti-form" id="arti-formId">
...snip...
itemDetails_HEREDOC;
这是在函数内部创建的吗?在这种情况下,全局
$sitePath
超出范围。@Michael,+1,哇——是的,herdoc在php函数中。另外5个已经正常工作了3周的php变量在herdeoc函数中。我在同一个globals.php文件中有很多这样的标签:class Labels{static public$usernamelab=“Username”;},我可以在php函数中使用类似sytax的标签::$usernamelab,没有问题,但是'Labels'类在globals.php的$sitePath旁边声明,在同一文件范围内。为什么会这样?我如何使$sitePath在php函数中可见?我在函数内部声明了“global$sitePath”(减少键入),BAM现在可以工作了——谢谢。问题——在globals.php中,我在全局范围中声明$sitePath——我也声明了:类标签{static public$usernamelab=“Username”},我不需要使用“global”限定符,在我可以使用的函数中:$foo=Labels::$USERNAMELABEL工作正常——即使Labels::$USERNAMELABELS与我的$sitePath处于同一全局文件范围?@want要在函数中使用全局,在每个函数中始终需要使用global
关键字(或使用$GLOBALS[]
取而代之的是我的偏好,或者修改函数以接收变量作为参数)标签::$USERNAMELABEL不需要全局
,因为标签::部分定义了它的范围。因此,不存在歧义。事实上,:
被称为范围操作符。
echo <<<itemDetails_HEREDOC
<img src="{$GLOBALS['sitePath']}/images/logo-landing2.png" />
<form action="$thisFilename" method="post" name="arti-form" id="arti-formId">
...snip...
itemDetails_HEREDOC;
function yourfunction($sitePath) {
// now $sitePath is in scope without
// reaching into the global namespace
}