【问题标题】:PHP variable is not working in a heredoc when adding a 6th添加第 6 个时 PHP 变量在 heredoc 中不起作用
【发布时间】: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 函数中可见?

标签: php html heredoc


【解决方案1】:

由于在函数内部调用了 HEREDOC,因此全局变量 $sitePath 不在作用域内。在函数的顶部,使用global 关键字:

function yourfunction() {
  global $sitePath;

  // Then build the HEREDOC
}

或者在 HEREDOC 中使用 $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 变量传递给需要它作为参数的函数。

function yourfunction($sitePath) {
  // now $sitePath is in scope without 
  // reaching into the global namespace
}

【讨论】:

  • 我在函数内部声明了 'global $sitePath'(少打字)和 BAM,它现在可以工作了——谢谢。问题——在我在全局范围内声明 $sitePath 的同一个 globals.php 中——我也声明了这个:class Labels { static public $USERNAMELABEL = "Username"; } 并且我不需要在我可以使用的函数中使用“全局”限定符: $foo = Labels::$USERNAMELABEL 并且它工作正常——即使 Labels ::$USERNAMELABELS 与我的 $sitePath 位于同一全局文件范围内?
  • @wantTheBest 要在函数中使用全局变量,您始终需要在每个函数中使用 global 关键字(或者使用 $GLOBALS[] 代替,我的偏好,或者修改函数以接收 var作为参数)
  • Labels::$USERNAMELABEL 不需要global,因为它的Labels:: 部分定义了它的范围。因此,没有歧义。事实上,:: 被称为 范围运算符
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 2020-12-01
  • 2011-07-16
  • 2016-12-27
  • 2012-07-01
  • 1970-01-01
  • 2019-02-16
相关资源
最近更新 更多