且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 PHP 中回显 HTML?

更新时间:2023-11-14 16:10:58

在 PHP 中有几种方法可以回显 HTML.

There are a few ways to echo HTML in PHP.

<?php if(condition){ ?>
     <!-- HTML here -->
<?php } ?>

2.在回声中

if(condition){
     echo "HTML here";
}

对于回显,如果您希望在 HTML 中使用双引号,则必须像这样使用单引号回显:

With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:

echo '<input type="text">';

或者你可以像这样逃避他们:

Or you can escape them like so:

echo "<input type="text">";

3.此处文档

4.Nowdocs(自 PHP 5.3.0 起)

模板引擎用于在主要包含 HTML 的文档中使用 PHP.事实上,PHP 的最初目的是成为一种模板语言.这就是为什么在 PHP 中您可以使用诸如短标签之类的东西来回显变量(例如 <?=$someVariable?>).

3. Heredocs

4. Nowdocs (as of PHP 5.3.0)

Template engines are used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>).

还有其他模板引擎(例如 Smarty、Twig 等)使语法更加简洁(例如 {{someVariable}}).

There are other template engines (such as Smarty, Twig, etc.) that make the syntax even more concise (e.g. {{someVariable}}).

使用模板引擎的主要好处是使设计(表示逻辑)与编码(业务逻辑).从长远来看,它还使代码更清晰,更易于维护.

The primary benefit of using a template engine is keeping the design (presentation logic) separate from the coding (business logic). It also makes the code cleaner and easier to maintain in the long run.

如果您还有其他问题,请随时发表评论.

If you have any more questions feel free to leave a comment.

可以在 PHP 文档中进一步阅读这些内容>.

注意: PHP 短标签 <??> 是不鼓励的,因为它们只有在启用 short_open_tag 时才可用 php.ini 配置文件指令,或者 PHP 配置了 --enable-short-tags 选项.从 5.4 开始,无论设置如何,它们都可用.

NOTE: PHP short tags <? and ?> are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option. They are available, regardless of settings from 5.4 onwards.