Heredocs 语法有问题吗?
我对定界文档的语法有疑问。让我先展示一下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果 MySQL 数据库中的数据为
NULL
,您将使用is_null()
函数进行检查,而不是与字符串'NULL'
进行比较:此外,正如其他人提到的,heredoc 的结尾不能缩进。
编辑:我刚刚在您的代码中编辑了一些其他内容,并展示了整个函数,以便您更好地了解。
If data in a MySQL database is
NULL
, you would check that with theis_null()
function, not comparing to the string'NULL'
: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.
我可以看到您的代码有几个问题,但是在涉及heredocs语法的情况下,请尝试删除
ENDOFRETURN;
之前的空格。I can see several problems with your code, but where the heredocs syntax is concerned, try removing the spaces before
ENDOFRETURN;
.有几个问题,正确的方法是这样的:
您看到:
ENDOFRETURN;
之前没有空格所有输出都在一个...返回中返回
There are several problems, the correct way would be something like:
you see:
ENDOFRETURN;
has no spaces before itall output is returned in one ... return
为了彻底起见:
NULL
是一个关键字,就像:print
、echo
、if
、<代码>为。同时,“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 anif
statement.如上所述,您在单引号中的 echo 语句不会执行您想要的操作。变量 $image 不会在单引号字符串中展开。将单引号和双引号反转为
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
Also the ENDOFRETURN; at closing your heredoc must not have any spaces before it.
只需调整
为
rtm - 完全正确和你的问题一样
simply adjust
to
rtm - exactly same problem as yours