get*_*way 0 html php image heredoc
我对heredoc的语法有问题.让我先说明代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
该$image变量来自于数据库,它是NULL或有一个文件名.如果图像为null我不想显示<img>标签,如果它有一个值,那么我想显示它.你知道我怎么解决这个问题吗?我一直在尝试一切,但没有任何工作呢!:))
如果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;
}
Run Code Online (Sandbox Code Playgroud)
此外,正如其他人所提到的,heredoc的结尾不得缩进.
编辑:我刚刚在你的代码中编辑了一些其他的东西,并展示了整个函数,以便给你一个更好的主意.
| 归档时间: |
|
| 查看次数: |
429 次 |
| 最近记录: |