Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php heredocs语法有问题吗?_Php_Html_Image_Heredoc - Fatal编程技术网

Php heredocs语法有问题吗?

Php heredocs语法有问题吗?,php,html,image,heredoc,Php,Html,Image,Heredoc,我对herdoc的语法有问题。让我先展示一下代码: 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">'

我对herdoc的语法有问题。让我先展示一下代码:

    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;

}
函数格式($user\u id、$user\u note、$image、$dt){
//这就是问题所在
如果($image=='NULL'){
//不显示图像
}
否则{
回声';
}

返回如果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;
}
函数格式($user\u id、$user\u note、$image、$dt)
{
如果(!为null($image)){
$image=“”;
}
返回只需调整即可

{spaces or tab}ENDOFRETURN;

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

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


我可以看到您的代码存在一些问题,但对于heredocs语法,请尝试删除
ENDOFRETURN;

前面的空格。如果存在一些问题,正确的方法可能是:

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;
}
函数格式($user\u id、$user\u note、$image、$dt){
//这就是问题所在
如果($image!==NULL){
$output.='';
}

$output.=如上所述,单引号中的echo语句不会执行您希望它执行的操作。变量$image将不会展开为单引号字符串。请按如下所示反转单引号和双引号

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

此外,在关闭时,您的heredoc前不得有任何空格。

为了彻底起见:


NULL
是一个关键字,就像:
print
echo
if
for
。同时,“NULL”(注意引号)是一个字符串,就像您键入了“if”(再次注意引号)一样它将是一个字符串,而不是
if
语句的开头。

对不起,我没有得到您打印并返回的相同函数中的代码?而且您的echo'echo''需要qoutes;'loool我也不理解我的代码!!!:)是的,我已经修改了它。对不起!!!您的主要问题是herdoc本身。永远不要使用它来输出HTML。PHP有自己的方法更可靠的方法你能给我一个例子吗请@colI仍然不知道你的实际问题是什么(除了代码中的错误).发生了什么?应该发生什么?谢谢你的回答,是的,但是我怎么才能显示$image!!当你在处理heredocs时,它会更复杂syntax@getaway:如其他人所述,请确保
ENDOFRETURN;
没有缩进。@getaway:我刚刚在代码中编辑了一些其他内容,并在中更好地显示了整个函数证明我想说的。谢谢,我喜欢干净的地板!!!但它仍然不起作用!!!它根本不显示图像!!!now@getaway:抱歉,我的代码中存在愚蠢的错误,请参阅编辑。我需要使用
"
不是
啊,我不明白!!你是什么意思:)是的,我知道我的真实代码是这样的,它显示在这里!!但是thanks@getaway:那么您看到的问题具体是什么?这没有问题,但我不想让它显示图像,如果它为空,则显示img src,否则这将显示
我想您没有看到的是OP的注意事项是,不检查
$image
是否等于字符串
“NULL”
您实际上是在检查它是否不等于未初始化的常量
NULL
。如果它不等于该常量,则将其添加到
$output
字符串中。该字符串将在函数末尾返回。除此之外,OP的原始函数存在许多问题,可能应该至少拆分为两个不同的函数。@sholsinger如他所说的“要么为空,要么有文件名”,所以这是完全有效的。我不同意这是有效的。我的意思是,我认为需要对OP进行更全面的解释。
echo "<img src='userpics/$image' width='480' height='480'>";