HTML 可以嵌入到 PHP 的“if”语句中吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/722379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can HTML be embedded inside PHP "if" statement?
提问by Frank Farmer
I would like to embed HTML inside a PHP if statement, if it's even possible, because I'm thinking the HTML would appear before the PHP if statement is executed.
如果可能的话,我想在 PHP if 语句中嵌入 HTML,因为我认为 HTML 会出现在 PHP if 语句执行之前。
I'm trying to access a table in a database. I created a pulldown menu in HTML that lists all the tables in the database and once I select the table from the pulldown, I hit the submit button.
我正在尝试访问数据库中的表。我在 HTML 中创建了一个下拉菜单,其中列出了数据库中的所有表格,一旦我从下拉列表中选择了表格,我就点击了提交按钮。
I use the isset function to see if the submit button has been pressed and run a loop in PHP to display the contents of the table in the database. So at this point I have the complete table but I want to run some more queries on this table. Hence the reason I'm trying to execute more HTML inside the if statement. Ultimately, I'm trying to either update (1 or more contents in a row or multiple rows) or delete (1 or more rows) contents in the table. What I'm trying to do is create another pulldown that corresponded to a column in a table to make the table search easier and radio buttons that correspond to whether I'd like to update or delete contents in the table.
我使用isset函数查看提交按钮是否被按下并在PHP中运行一个循环来显示数据库中表格的内容。所以此时我有完整的表,但我想在这个表上运行更多的查询。因此,我试图在 if 语句中执行更多 HTML 的原因。最终,我试图更新(一行或多行中的 1 个或多个内容)或删除(1 个或多个行)表中的内容。我想要做的是创建另一个与表中的列相对应的下拉列表,以使表搜索更容易,并创建与我是否要更新或删除表中的内容相对应的单选按钮。
回答by Frank Farmer
<?php if($condition) : ?>
<a href="http://yahoo.com">This will only display if $condition is true</a>
<?php endif; ?>
By request, here's elseif and else (which you can also find in the docs)
根据要求,这里是 elseif 和 else(您也可以在文档中找到)
<?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; ?>
It's that simple.
就这么简单。
The HTML will only be displayed if the condition is satisfied.
只有在满足条件时才会显示 HTML。
回答by jgallant
Yes,
是的,
<?php
if ( $my_name == "someguy" ) {
?> HTML GOES HERE <?php;
}
?>
回答by chaos
Yes.
是的。
<? if($my_name == 'someguy') { ?>
HTML_GOES_HERE
<? } ?>
回答by Marki555
Using PHP close/open tags is not very good solution because of 2 reasons: you can't print PHP variables in plain HTML and it make your code very hard to read (the next code block starts with an end bracket }, but the reader has no idea what was before).
使用 PHP 关闭/打开标签并不是一个很好的解决方案,原因有二:您不能在纯 HTML 中打印 PHP 变量,这会使您的代码难以阅读(下一个代码块以结束括号开头},但读者有不知道以前是什么)。
Better is to use heredocsyntax. It is the same concept as in other languages (like bash).
更好的是使用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_TEXTis your delimiter (it can be basically any text like EOF, EOT). Everything between is considered string by PHP as if it were in double quotes, so you can print variables, but you don't have to escape any quotes, so it very convenient for printing html attributes.
END_OF_TEXT是您的分隔符(它基本上可以是任何文本,如 EOF、EOT)。PHP 将两者之间的所有内容视为字符串,就像在双引号中一样,因此您可以打印变量,但不必转义任何引号,因此打印 html 属性非常方便。
Note that the closing delimiter must begin on the start of the line and semicolon must be placed right after it with no other chars (END_OF_TEXT;).
请注意,结束定界符必须在行的开头开始,并且分号必须紧跟在它之后,没有其他字符 ( END_OF_TEXT;)。
Heredoc with behaviour of string in single quotes (') is called nowdoc. No parsing is done inside of nowdoc. You use it in the same way as heredoc, just you put the opening delimiter in single quotes - echo <<< 'END_OF_TEXT'.
在单引号 ( ') 中具有字符串行为的 Heredoc称为nowdoc。nowdoc 内部没有进行任何解析。您可以像heredoc一样使用它,只是将开始分隔符放在单引号中 - echo <<< 'END_OF_TEXT'。
回答by Fast Login
So if condition equals the value you want then the php document will run "include" and include will add that document to the current window for example:
因此,如果条件等于您想要的值,那么 php 文档将运行“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')}
?>
`
`
回答by Bruce
I know this is an old post, but I really hate that there is only one answer here that suggests not mixing html and php. Instead of mixing content one should use template systems, or create a basic template system themselves.
我知道这是一篇旧帖子,但我真的很讨厌这里只有一个答案建议不要混合使用 html 和 php。与其混合内容,不如使用模板系统,或者自己创建一个基本的模板系统。
In the 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;
?>
In the html (/path/to/file.html)
在 html (/path/to/file.html)
<p>##variable1## ate ##variable2## for ##variable3## with ##variable4##.</p>
The output of this would be:
其输出将是:
Alice ate apples for lunch with Bob.
回答by govindak
<?php if ($my_name == 'aboutme') { ?>
HTML_GOES_HERE
<?php } ?>

