HTML可以嵌入到PHP“ if”语句中吗?


120

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

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

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


Answers:


371
<?php if($condition) : ?>
    <a href="http://yahoo.com">This will only display if $condition is true</a>
<?php endif; ?>

根据要求,这里是elseif和else(您也可以在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。


“ ...(您也可以在[docs] [1]中找到)”您所说的这些文档到底在哪里?
gmeben

39

是,

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

15

是。

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

6
有些服务器没有安装libxml或pdo_mysql,但是我们仍然可以推荐使用它们的解决方案。
混乱

2
我认为这应该只是对乔恩的回答的评论。
Rimas Kudelis 09年

3
嗯 您想告诉我如何将格式代码嵌入注释中吗?
混沌

3

使用PHP close / open标签不是一个很好的解决方案,这有两个原因:您不能以纯HTML格式打印PHP变量,这会使您的代码难以阅读(下一个代码块以方括号开头},但是读者可以不知道以前是什么)。

更好的方法是使用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 <<< 'END_OF_TEXT'


非常感谢您提醒我这一点。Heredoc对于具有多个条件的更复杂的HTML极为有用,并且比转义属性和引号要容易得多!
UTCWebDev

2

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

`

<?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')}
?>

`



0

我知道这是一篇旧文章,但我真的很讨厌这里只有一个答案,建议不要混合使用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.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.