【问题标题】:Can HTML be embedded inside PHP "if" statement?HTML 可以嵌入到 PHP 的“if”语句中吗?
【发布时间】:2010-10-17 20:49:01
【问题描述】:

如果可能的话,我想在 PHP if 语句中嵌入 HTML,因为我认为 HTML 会出现在 PHP if 语句执行之前。

我正在尝试访问数据库中的表。我在 HTML 中创建了一个下拉菜单,其中列出了数据库中的所有表,一旦我从下拉菜单中选择了表,我点击了提交按钮。

我使用 isset 函数查看是否已按下提交按钮并在 PHP 中运行一个循环以显示数据库中表的内容。所以此时我有完整的表,但我想在这个表上运行更多查询。因此,我试图在 if 语句中执行更多 HTML 的原因。最终,我尝试更新(一行或多行中的 1 个或多个内容)或删除表中的(1 个或多个行)内容。我要做的是创建另一个与表中的列相对应的下拉菜单,以使表搜索更容易,并创建与我是否要更新或删除表中的内容相对应的单选按钮。

【问题讨论】:

标签: php html


【解决方案1】:

是的,

<?php if ( $my_name == "someguy" ) { ?> 
    HTML GOES HERE 
<?php } ?>

【讨论】:

    【解决方案2】:

    我知道这是一篇旧帖子,但我真的很讨厌这里只有一个答案建议不要混合 html 和 php。与其混合内容,不如使用模板系统,或者自己创建一个基本的模板系统。

    在php中

    <?php 
      $var1 = 'Alice'; $var2 = 'apples'; $var3 = 'lunch'; $var4 = 'Bob';
    
      if ($var1 == 'Alice') {
        $html = file_get_contents('/path/to/file.html'); //get the html template
        $template_placeholders = array('##variable1##', '##variable2##', '##variable3##', '##variable4##'); // variable placeholders inside the template
        $template_replace_variables = array($var1, $var2, $var3, $var4); // the variables to pass to the template
        $html_output = str_replace($template_placeholders, $template_replace_variables, $html); // replace the placeholders with the actual variable values.
      }
    
      echo $html_output;
    ?>
    

    在html中(/path/to/file.html)

    <p>##variable1## ate ##variable2## for ##variable3## with ##variable4##.</p>
    

    这样的输出是:

    Alice ate apples for lunch with Bob.
    

    【讨论】:

      【解决方案3】:

      因此,如果条件等于您想要的值,则 php 文档将运行“包含” 并包含将该文档添加到当前窗口 例如:

      `

      <?php
      $isARequest = true;
      if ($isARequest){include('request.html');}/*So because $isARequest is true then it will include request.html but if its not a request then it will insert isNotARequest;*/
      else if (!$isARequest) {include('isNotARequest.html')}
      ?>
      

      `

      【讨论】:

        【解决方案4】:

        使用 PHP 关闭/打开标签不是很好的解决方案,原因有两个:您无法在纯 HTML 中打印 PHP 变量,并且它使您的代码很难阅读(下一个代码块以结束括号 @987654322 开头@,但读者不知道之前是什么)。

        最好使用heredoc 语法。它与其他语言(如 bash)中的概念相同。

         <?php
         if ($condition) {
           echo <<< END_OF_TEXT
             <b>lots of html</b> <i>$variable</i>
             lots of text...
         many lines possible, with any indentation, until the closing delimiter...
         END_OF_TEXT;
         }
         ?>
        

        END_OF_TEXT 是您的分隔符(它基本上可以是任何文本,如 EOF、EOT)。之间的所有内容都被PHP视为字符串,就好像它在双引号中一样,因此您可以打印变量,但您不必转义任何引号,因此打印html属性非常方便。

        请注意,结束分隔符必须从行首开始,分号必须紧跟其后,不能有其他字符 (END_OF_TEXT;)。

        具有单引号 (') 字符串行为的 Heredoc 称为 nowdoc。 nowdoc 内部不进行任何解析。使用方式与 heredoc 相同,只是将开始分隔符放在单引号中 - echo &lt;&lt;&lt; 'END_OF_TEXT'

        【讨论】:

        • 非常感谢您提醒我这一点。 Heredoc 对于具有多个条件的更复杂的 HTML 非常有帮助,并且比转义属性和引号要容易得多!
        【解决方案5】:

        是的。

        <?  if($my_name == 'someguy') { ?>
                HTML_GOES_HERE
        <?  } ?>
        

        【讨论】:

        • 有些服务器没有安装 libxml 或 pdo_mysql,但我们仍然可以推荐使用它们的解决方案。
        • 我认为这应该只是对乔恩回答的评论。
        • 嗯。你想告诉我我是如何在评论中嵌入格式化代码的吗?
        【解决方案6】:
        <?php if ($my_name == 'aboutme') { ?>
            HTML_GOES_HERE
        <?php } ?>
        

        【讨论】:

        • 请在您的回答中添加一些描述。
        • 描述可能是“复制粘贴 5 岁的答案”:P
        【解决方案7】:
        <?php if($condition) : ?>
            <a href="http://yahoo.com">This will only display if $condition is true</a>
        <?php endif; ?>
        

        根据要求,这里是 elseif 和 else(您也可以在 the docs 中找到)

        <?php if($condition) : ?>
            <a href="http://yahoo.com">This will only display if $condition is true</a>
        <?php elseif($anotherCondition) : ?>
            more html
        <?php else : ?>
            even more html
        <?php endif; ?>
        

        就这么简单。

        只有满足条件时才会显示 HTML。

        【讨论】:

        • "...(您也可以在 [the docs][1] 中找到)" 您所说的这些文档到底在哪里?
        猜你喜欢
        • 2017-02-15
        • 1970-01-01
        • 2014-01-13
        • 2016-04-12
        • 1970-01-01
        • 2013-08-18
        • 1970-01-01
        • 2020-11-09
        相关资源
        最近更新 更多