【问题标题】:PHP Print and Echo HTMLPHP 打印和回显 HTML
【发布时间】:2011-05-01 06:56:08
【问题描述】:

非常简单的问题。大声笑我很尴尬地问这个,因为我通常对 php 非常好,但是在 php 中显示 html 的方法是什么?例如:

<? if($flag): ?> <div>This will show is $flag is true </div> <? endif; ?>

<? if($flag) echo '<div>This will show is $flag is true </div>'; ?>

我知道至少还有 2 种其他方式我无法记住它们 atm... 帮助是定义。提前赞赏!! =D

【问题讨论】:

标签: php html printing echo nowdoc


【解决方案1】:

您也可以使用heredoc

【讨论】:

    【解决方案2】:

    heredoc 的用法如下:

    if($flag)
    {
        echo <<<HTML
            <div>This will show if \$flag is true </div>
    HTML;
    
    }
    

    如果你不想要变量插值,你必须像我上面所说的那样转义可能的变量名。或者,您可以在 PHP 5.3 及更高版本中使用 nowdoc

    if($flag)
    {
        echo <<<'HTML'
            <div>This will show if $flag is true </div>
    HTML;
    
    }
    

    【讨论】:

      【解决方案3】:

      PHP 还有一个nowdoc 语法,其工作方式类似于heredoc,但类似于单引号字符串,这些文档块不会被解析。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 2011-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-21
        • 2012-07-11
        相关资源
        最近更新 更多