我在3周前写了一个heredoc,它工作得很好,直到今天它还使用了5个php变量.
这是heredoc:
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;
Run Code Online (Sandbox Code Playgroud)
上面的heredoc中有六(6)个php变量.第一个($ sitePath)不工作.我今天刚加了.
由于某种原因,服务器没有正确替换'$ sitePath'(我的heredoc中的第一个php变量),因为当浏览器收到上述页面时,会产生错误:
Notice: Undefined variable: sitePath in C:\xampp\htdocs\Arti-facks\showItem.php on line 221
Run Code Online (Sandbox Code Playgroud)
以下是heredoc中的5个php变量,并且工作正常3周 - 在将html发送到我的浏览器之前,它们正在被正确替换为它们的值:
我怎么知道上面的php变量在服务器上正确解析,其关联值被放入heredoc然后发送到浏览器?
很简单 - 我做了一个'查看页面源代码'而不是在服务器发送的html代码中看到'$ thisFilename'或'$ itemName'等 - 我在上面的heredoc html中看到了它们的关联值.
例如,页面源显示heredoc 中的' $ thisFilename '已正确解析为" showItem.php".
我的'$ sitePath'在名为" globals.php " 的全局包含文件中声明,在该文件中,$ sitePath声明如下:
$sitePath = "http://localhost/Arti-facks";
Run Code Online (Sandbox Code Playgroud)
在showItem.php的顶部,在过去的3周里,我有这个包含语句来引入globals.php:
require_once 'globals.php'; // variables and statics used throughout
Run Code Online (Sandbox Code Playgroud)
所以今天我在globals.php中添加了$ sitePath声明(上面)
为了证明showItem.php能够"看到"我添加到globals.php的新声明的$ sitePath,我回应变量 - 确定,showItem.php 100%能够'看到'我新申报的$ sitePath.
并且 - 尽管如此 - 尽管使用上面的heredoc和其他五个PHP变量已经有3个星期 - 但是heredoc并没有得到$ sitePath.
当我查看页面源'这是我在上面的heredoc的$ sitePath行中看到的内容:
<img src="/images/logo-landing2.png" />
Run Code Online (Sandbox Code Playgroud)
您可以看到/images/logo-landing2.png之前的$ sitePath部分是BLANK或缺失.
为什么我,在3个无问题的星期,在上面的heredoc中获得了五个成功解析的PHP变量,但是今天我添加了第6个php变量,就好像
"Goodness, your quota for PHP variables in your heredoc there was maxed out
at 5. And now we see you're trying to use a 6th php variable -- what were you thinking."
Run Code Online (Sandbox Code Playgroud)
由于HEREDOC在函数内部被调用,因此全局变量$sitePath不在范围内.在函数的顶部,使用global关键字:
function yourfunction() {
global $sitePath;
// Then build the HEREDOC
}
Run Code Online (Sandbox Code Playgroud)
或者$GLOBALS在HEREDOC中使用数组:
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;
Run Code Online (Sandbox Code Playgroud)
或者优先选择任一选项,将$sitePath变量传递给需要它作为参数的函数.
function yourfunction($sitePath) {
// now $sitePath is in scope without
// reaching into the global namespace
}
Run Code Online (Sandbox Code Playgroud)