【发布时间】:2012-03-25 14:31:39
【问题描述】:
我在三周前写了一个很好的heredoc,直到今天它使用了五个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;
上述 heredoc 中有六 (6) 个 PHP 变量。第一个 ($sitePath) 不起作用。我今天才加的。
由于某种原因,服务器没有正确替换“$sitePath”(我的 heredoc 中的第一个 PHP 变量),因为当浏览器接收到上述页面时,会生成错误:
Notice: Undefined variable: sitePath in C:\xampp\htdocs\Arti-facks\showItem.php on line 221
这里有五个 PHP 变量,它们已在 heredoc 中并正常工作了三周——它们被正确替换为它们的值 在 HTML 发送到我的浏览器之前:
- $这个文件名
- $theLoggedInUser
- $itemName
- $theLinkID
- $itemEra
我怎么知道上面的 PHP 变量在服务器上被正确解析,它们的相关值被放入 heredoc 然后发送到浏览器?
简单——我做了一个“查看页面源代码”,而不是在服务器发送的 HTML 代码中看到“$thisFilename”或“$itemName”等——而是在上面看到它们的关联值heredoc HTML 内容。
例如,页面源代码显示 heredoc 中的“$thisFilename”已正确解析为“showItem.php”。
我的“$sitePath”在名为“globals.php”的全局包含文件中声明,在该文件中,$sitePath 声明如下:
$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。
然而——尽管如此——尽管使用上述heredoc和它的五个其他 PHP变量三周—— heredoc 没有获得 $sitePath。
当我“查看页面源代码”时,我看到上面 heredoc 的 $sitePath 行:
<img src="/images/logo-landing2.png" />
您可以看到/images/logo-landing2.png 之前的$sitePath 部分是空白 或缺失。
为什么我已经连续三个没有问题的周,在上面的 heredoc 中获得了 五个 成功解析的 PHP 变量,但我今天添加了第 6 个 PHP 变量,就好像
“天哪,您在 heredoc 中的 PHP 变量配额已达到 5 个上限。现在我们看到您正在尝试使用第 6 个 PHP 变量——您在想什么。”
【问题讨论】:
-
这个HEREDOC是在函数内部创建的吗?在这种情况下,全局
$sitePath超出范围。 -
@Michael,+1,哇——是的,heredoc 在 php 函数中。并且已经正常工作了 3 周的其他 5 个 php 变量在带有 heredoc 的函数中。我在同一个 globals.php 文件中有很多这些:class Labels { static public $USERNAMELABEL = "Username";我可以在php函数中使用像Labels::$USERNAMELABEL这样的语法没有问题,但是'Labels'类在我的$sitePath旁边声明,在globals.php中相同的文件范围 .为什么会这样以及如何使 $sitePath 在 php 函数中可见?