Heredocs 语法有问题吗?

发布于 2024-10-10 12:18:50 字数 913 浏览 3 评论 0原文

我对定界文档的语法有疑问。让我先展示一下代码:

    function format($user_id,$user_note,$image,$dt){

        //this is the problem
      if($image=='NULL'){
     //don't display image
     }

     else {
  echo '<img src="userpics/$image" width="480" height="480">';
}
         return <<<ENDOFRETURN
       <div class ="commentbox">
                         $date
                         </div>
                             <div class="leftpanel">

                           $user_note
                        $image
                                 <div class="date">
                                    $rel
                                 </div>
                             </div>
    ENDOFRETURN;

}

$image 变量来自数据库,它要么为 NULL,要么有一个文件名。如果图像为空,我不想显示 标签,如果它确实有值,那么我想显示它。你知道我该如何解决这个问题吗?我已经尝试了一切,但还没有任何效果! :))

I have a problem with the syntax for a heredoc. Let me show the code first:

    function format($user_id,$user_note,$image,$dt){

        //this is the problem
      if($image=='NULL'){
     //don't display image
     }

     else {
  echo '<img src="userpics/$image" width="480" height="480">';
}
         return <<<ENDOFRETURN
       <div class ="commentbox">
                         $date
                         </div>
                             <div class="leftpanel">

                           $user_note
                        $image
                                 <div class="date">
                                    $rel
                                 </div>
                             </div>
    ENDOFRETURN;

}

The $image variable comes from the database, it's either NULL or has a filename. If an image is null I don't want to display the <img> tag, if it does have a value then I want to show it. Do you know how I can solve this problem? I have been trying everything, but nothing has worked yet!! :))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千鲤 2024-10-17 12:18:50

如果 MySQL 数据库中的数据为 NULL,您将使用 is_null() 函数进行检查,而不是与字符串 'NULL' 进行比较:

function format($user_id,$user_note,$image,$dt)
{
  if(!is_null($image)){
     $image = "<img src=\"userpics/$image\" width=\"480\" height=\"480\">";
  }
  return <<<ENDOFRETURN
   <div class ="commentbox">$date</div>
   <div class="leftpanel">
     $user_note
     $image
     <div class="date">$rel</div>
   </div>
ENDOFRETURN;
}

此外,正如其他人提到的,heredoc 的结尾不能缩进。

编辑:我刚刚在您的代码中编辑了一些其他内容,并展示了整个函数,以便您更好地了解。

If data in a MySQL database is NULL, you would check that with the is_null() function, not comparing to the string 'NULL':

function format($user_id,$user_note,$image,$dt)
{
  if(!is_null($image)){
     $image = "<img src=\"userpics/$image\" width=\"480\" height=\"480\">";
  }
  return <<<ENDOFRETURN
   <div class ="commentbox">$date</div>
   <div class="leftpanel">
     $user_note
     $image
     <div class="date">$rel</div>
   </div>
ENDOFRETURN;
}

Also, as others have mentioned, the end of the heredoc must not be indented.

EDIT: I just edited a few other things in your code and showed the whole function to give you a better idea.

吾性傲以野 2024-10-17 12:18:50

我可以看到您的代码有几个问题,但是在涉及heredocs语法的情况下,请尝试删除 ENDOFRETURN; 之前的空格。

I can see several problems with your code, but where the heredocs syntax is concerned, try removing the spaces before ENDOFRETURN;.

人心善变 2024-10-17 12:18:50

有几个问题,正确的方法是这样的:

function format($user_id,$user_note,$image,$dt){

    //this is the problem
    if($image !== NULL){
        $output .= '<img src="userpics/' . $image . '" width="480" height="480">';
    }
    $output .= <<<ENDOFRETURN
    <div class ="commentbox">
                     $date
                     </div>
                         <div class="leftpanel">

                       $user_note
                    $image
                             <div class="date">
                                $rel
                             </div>
                         </div>
ENDOFRETURN;

    return $output;
}

您看到:

  • ENDOFRETURN; 之前没有空格

  • 所有输出都在一个...返回中返回

There are several problems, the correct way would be something like:

function format($user_id,$user_note,$image,$dt){

    //this is the problem
    if($image !== NULL){
        $output .= '<img src="userpics/' . $image . '" width="480" height="480">';
    }
    $output .= <<<ENDOFRETURN
    <div class ="commentbox">
                     $date
                     </div>
                         <div class="leftpanel">

                       $user_note
                    $image
                             <div class="date">
                                $rel
                             </div>
                         </div>
ENDOFRETURN;

    return $output;
}

you see:

  • ENDOFRETURN; has no spaces before it

  • all output is returned in one ... return

贵在坚持 2024-10-17 12:18:50

为了彻底起见:

NULL是一个关键字,就像:printechoif、<代码>为。同时,“NULL”(注意引号)是一个字符串,就像您输入“if”(再次注意引号)一样,它也是一个字符串,而不是 if 语句的开头。

For the sake of being thorough:

NULL is a keyword, just like: print, echo, if, for. Meanwhile,'NULL' (note the quotes) is a string, just like if you had typed 'if' (note the quotes, again) it would be a string, not the beginning of an if statement.

长伴 2024-10-17 12:18:50

如上所述,您在单引号中的 echo 语句不会执行您想要的操作。变量 $image 不会在单引号字符串中展开。将单引号和双引号反转为

echo "<img src='userpics/$image' width='480' height='480'>";

Also the ENDOFRETURN;在关闭你的heredoc之前不能有任何空格。

Your echo statement in single quotes as above, won't do what you intend it to. The variable $image will not be expanded in a single-quoted string. Reverse your single and double quoting instead as

echo "<img src='userpics/$image' width='480' height='480'>";

Also the ENDOFRETURN; at closing your heredoc must not have any spaces before it.

混浊又暗下来 2024-10-17 12:18:50

只需调整

{spaces or tab}ENDOFRETURN;

{no spaces/tab}ENDOFRETURN;

rtm - 完全正确和你的问题一样

需要注意的是,带有结束标识符的行不得包含任何其他字符,分号 (;) 除外。这尤其意味着标识符不能缩进,并且分号前后不能有任何空格或制表符。同样重要的是要认识到结束标识符之前的第一个字符必须是本地操作系统定义的换行符。在 UNIX 系统(包括 Mac OS X)上,这是 \n。结束分隔符(可能后跟分号)后面还必须跟有换行符。

如果这条规则被破坏并且结束标识符不是“干净的”,则它将不会被视为结束标识符,并且 PHP 将继续寻找结束标识符。如果在当前文件末尾之前找不到正确的结束标识符,则最后一行将导致解析错误。

simply adjust

{spaces or tab}ENDOFRETURN;

to

{no spaces/tab}ENDOFRETURN;

rtm - exactly same problem as yours

It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文