PHP:如何隐藏/显示 HTML 块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5666788/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:08:55  来源:igfitidea点击:

PHP: How to hide/display chunks of HTML

phphtml

提问by Chuck Le Butt

I'm hoping someone can help with this very newbie question. I've just come from an ASP.Net environment where it was easy to hide or show chunks of HTML. For example: Different stages of a form, depending on what the user had entered.

我希望有人可以帮助解决这个非常新手的问题。我刚从 ASP.Net 环境中来,在那里很容易隐藏或显示 HTML 块。例如:表单的不同阶段,取决于用户输入的内容。

Hiding or showing <div>/<asp:Panel>s was very simple, but in PHP all I can think to do is put my HTML into an echostatement. This makes the HTML very difficult to read, difficult to maintain and the whole file looks rather messy.

隐藏或显示<div>/ <asp:Panel>s 非常简单,但在 PHP 中我能想到的就是将我的 HTML 放入一个echo语句中。这使得 HTML 非常难以阅读、难以维护并且整个文件看起来相当混乱。

I'm positive there must be a better way of hiding or showing chunks of HTML without having to involve an echostatement, but I can't find any online documentation that explains how.

我很肯定必须有更好的方法来隐藏或显示 HTML 块而不必涉及echo语句,但我找不到任何解释如何操作的在线文档。

Thanks for any tips, advice or links to good PHP resources for this level of problem.

感谢您提供针对此级别问题的任何提示、建议或指向良好 PHP 资源的链接。

回答by Shoe

Considering PHP as an embedded language you should, in order to get better readability, use the specific language forms for the "templating".

将 PHP 视为嵌入式语言,为了获得更好的可读性,您应该为“模板”使用特定的语言形式。

Instead of using echo you could just write the HTML outside the <?php ?>tags (never use <? ?>which could be misleading).

您可以不使用 echo,而是在<?php ?>标签外编写 HTML (切勿使用<? ?>可能会产生误导的内容)。

Also it is suggested to use if () :instead of if () {, for example:

还建议使用if () :代替if () {,例如:

<body>
    <?php if (true) : ?>
        <p>It is true</p>
    <?php endif; ?>
</body>

References:

参考:

回答by David

You don't need to put the HTML into an echostatement. Think of the HTML as implicitly being echoed. So, for conditionally showing a chunk of HTML, you'd be looking for a construct like this:

您不需要将 HTML 放入echo语句中。将 HTML 视为隐式被echo编辑。因此,为了有条件地显示一段 HTML,您需要寻找这样的结构:

<?php
    if (condition == true) {
?>
    <div>
        <p>Some content</p>
    </div>
<?php
    }
?>

So the HTML string literal that exists outside of the PHP tags is just implicitly delivered to the page, but you can wrap logic around it to drive it. In the above, the divand its contents are all within the scope of the ifstatement, and so that chunk of HTML (even though it's outside of the PHP tags) is only delivered if the condition in the PHP code is true.

因此,存在于 PHP 标记之外的 HTML 字符串文字只是隐式传递到页面,但您可以围绕它包装逻辑来驱动它。在上面, thediv及其内容都在if语句的范围内,因此只有在 PHP 代码中的条件为true.

回答by Nik

Try this:

尝试这个:

<?php if ($var == true) { ?>
    <table>...</table>
<?php } ?>

You can use PHP tags like wrapped around HTML and based on the conditional the HTML will either be rendered or it won't.

您可以使用 PHP 标签,例如环绕 HTML 并基于条件 HTML 将被呈现或不会呈现。

回答by KRM

This should work too. E.g. for hiding/displaying a table tag.

这也应该有效。例如用于隐藏/显示表格标签。

<table <?php echo (condition == true)?'visible':'hidden'; ?> ></table>