如何将条件检查与 php Heredoc 语法结合起来?
我使用heredoc语法来填充一些输出内容。
在准备这些内容时,我需要检查一些条件来决定是否添加它们。
当我尝试时,它显示错误..
echo <<<END_HTML
<div class="menu_form_body">
<table style="font-family:arial; font-size:12px; color:#ffffff;">
<tr>
<td colspan="2" align="center"><center><b>{$title}</b></center></td>
</tr>
<tr>
<td>{$filter[$key]['street']}<br/>
{$filter[$key]['city']}, {$filter[$key]['state']}<br/>
{$filter[$key]['zip']}<br/>
</td>
<td nowrap>{$filter[$key]['phone']}<br/>
{$filter[$key]['email']} // here i need to check for some condition
</td>
</tr>
END_HTML;
所以我尝试了
....
<td nowrap>{$filter[$key]['phone']}<br/>
END_HTML;
if($filter[$key]['display_email']!='No'){
echo <<<END_HTML
{$filter[$key]['email']}
END_HTML;
}
echo <<<END_HTML
....
...
END_HTML;
可能是什么问题?
i use a heredoc syntax to populate some content for output.
While preparing these content i need to check for some conditions to decide whether to add them or not.
While i tried that, it shows errors..
echo <<<END_HTML
<div class="menu_form_body">
<table style="font-family:arial; font-size:12px; color:#ffffff;">
<tr>
<td colspan="2" align="center"><center><b>{$title}</b></center></td>
</tr>
<tr>
<td>{$filter[$key]['street']}<br/>
{$filter[$key]['city']}, {$filter[$key]['state']}<br/>
{$filter[$key]['zip']}<br/>
</td>
<td nowrap>{$filter[$key]['phone']}<br/>
{$filter[$key]['email']} // here i need to check for some condition
</td>
</tr>
END_HTML;
so i tried
....
<td nowrap>{$filter[$key]['phone']}<br/>
END_HTML;
if($filter[$key]['display_email']!='No'){
echo <<<END_HTML
{$filter[$key]['email']}
END_HTML;
}
echo <<<END_HTML
....
...
END_HTML;
what may be the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我什至不确定这在heredoc声明中是否可能。但除此之外,它实在是丑陋且难以阅读。你不想这样做。
为什么你的定界符中需要一个条件?您可以在执行heredoc语句之前进行条件评估。
I am not even sure if this is possible inside a heredoc statement. But besides that it's just plain ugly and unreadable. You do not want to do this.
Why do you need a condition inside your heredoc? You could do the condition evaluation before you execute the heredoc statement.
确保您的END_HTML 中没有任何内容;语句是缩进的。
另外,为什么你必须使用heredoc呢?为什么不直接做这样的事情:
Make sure none of your END_HTML; statements are indented.
Also, why do you have to use heredoc for this? Why not just do something like: