gpt4 book ai didi

添加第 6 个时,PHP 变量在 heredoc 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 15:20:43 26 4
gpt4 key购买 nike

三周前我写了一个 heredoc,它运行良好,直到今天它使用了五个 PHP 变量。

这里是文档:

   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

以下是 heredoc 中的五个 PHP 变量,它们在三周内都运行良好 —— 在 HTML 发送到我的浏览器之前,它们被正确地替换为它们的值:

  • $这个文件名
  • $theLoggedInUser
  • $项目名称
  • $链接ID
  • $itemEra

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

简单——我执行“查看页面源代码”,而不是在服务器发送的 HTML 代码中看到“$thisFilename”或“$itemName”等——我在上面看到它们的关联值heredoc HTML 内容。

例如,页面源代码显示 heredoc 中的“$thisFilename”已正确解析为“showItem.php”。

我的“$sitePath”在名为“globals.php”的全局包含文件中声明,在该文件中,$sitePath 声明如下:

  $sitePath = "http://localhost/Arti-facks";

在过去的三周里,在 showItem.php 的顶部,我有这个 include 语句来拉入 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 变量,就好像

"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."

最佳答案

由于 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
}

关于添加第 6 个时,PHP 变量在 heredoc 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9612057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com