【问题标题】:Echo a if statement in the middle of a echo在回显中间回显 if 语句
【发布时间】:2015-02-09 22:03:07
【问题描述】:

我有一个带有复选框的代码:

Echo "<p>Some text go here (I NEED TO PUT HERE CHECKBOX CHECKED FROM IF), here goes another text.</p>"

if( isset( $_POST['check'] ))
{
    if( count( $_POST['check'] ) > 0 )
    {
        echo "" . implode(", ", $_POST['check']);
    }

我需要将复选框数组中的 if 放在 echo 中间,有办法吗?

Tks

【问题讨论】:

  • echo "some stuff"; if( x) { echo "conditional stuff"; } echo "rest of stuff"; 由于某种原因无法正常工作?
  • $checkboxes = ''; if (...){$checkboxes = implode(...) } echo "All stuff with ($checkboxes) inbetween"; ?

标签: php html if-statement checkbox


【解决方案1】:

如果将值分配给变量,则可以将其连接(呃!这个词...)到一个 echo 命令。

if( isset( $_POST['check'] ))
{
    if( count( $_POST['check'] ) > 0 )
    {
        $var = implode(", ", $_POST['check']);
    } else {
        $var = "";
    }

echo "<p>Some text go here" . $var . ", here goes another text.</p>";

编辑

添加Mark的评论,作为上述回显命令的更有效方式:

echo "<p>Some text go here", $var, ", here goes another text.</p>";

【讨论】:

  • 或者甚至只是回显几个元素:echo "&lt;p&gt;Some text go here", $var, ", here goes another text.&lt;/p&gt;";....不同之处在于该语句不需要任何连接
  • @MarkBaker 从来没有这样用过,谢谢你的评论! :)
  • 节省一点点连接所需的内存和处理器开销
  • @MarkBaker 所以,我不知道,我把它放在我的代码上并且我的代码不能正常工作。你能帮帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 2023-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多