[PHP变量在添加第六号时在heredoc中不起作用

问题描述 投票:2回答:1

三周前,我写了一个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发送到了我的浏览器:

  • $ thisFilename
  • $ theLoggedInUser
  • $ itemName
  • $ theLinkID
  • $ itemEra

我怎么知道上面的PHP变量已在服务器上正确解析,并将它们的关联值放入heredoc,然后发送到浏览器?

简单-我做一个'view page source',而不是在服务器发送的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。

And yet

-尽管如此,尽管使用上述heredoc及其5个其他 PHP变量三个星期-Heredoc是not获取$ sitePath。

[当我'查看页面源代码时,这是我在上面heredoc的$ sitePath行中看到的内容:

    <img src="/images/logo-landing2.png" />

您可以看到/images/logo-landing2.png之前的$ sitePath部分为空白

或丢失。

为什么我连续三个星期没有问题,在上面的heredoc中已经成功解析了[[5]]个PHP变量,但今天我添加了第6个PHP变量,就好像

“好,您在Heredoc中对PHP变量的配额已满5。现在我们看到您正在尝试使用第6个PHP变量-您在想什么。”

我三周前写了一个Heredoc,效果很好,直到今天它使用了五个PHP变量。这是heredoc:echo <<<< itemdetails_heredoc src =“%24sitePath / images / ...”>

php html heredoc
1个回答
5
投票
由于HEREDOC在函数内部被调用,因此全局变量$sitePath不在范围内。在函数顶部,使用global关键字:

function yourfunction() { global $sitePath; // Then build the HEREDOC }

© www.soinside.com 2019 - 2024. All rights reserved.