在 PHP 中回显 HTML 的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2347887/
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
Best way to echo HTML in PHP
提问by Alex
I'm a fairly experienced PHP coder, and am just wondering what the best way of echoing large chunks of HTML code is (best practise).
我是一个相当有经验的 PHP 编码员,我只是想知道回显大量 HTML 代码的最佳方式是什么(最佳实践)。
Is it better to do this:
这样做是否更好:
<?php
echo "<head>
<title>title</title>
<style></style>
</head>";
?>
or this:
或这个:
<?php
define("rn","\r\n");
echo "<head>".rn
."<title>title</title>".rn
."<style></style".rn
."</head>".rn;
?>
I tend to use the second as it doesn't mess up indenting in the php source. Is this the way most people do it?
我倾向于使用第二个,因为它不会在 php 源代码中弄乱缩进。这是大多数人的做法吗?
回答by Doug T.
IMO, The best way is typically to store the HTML separately in a template file. This is a file that typically contains HTML with some fields that need to get filled in. You can then use some templating framework to safely fill in the fields in the html document as needed.
IMO,最好的方法通常是将 HTML 单独存储在模板文件中。这是一个通常包含 HTML 的文件,其中包含一些需要填充的字段。然后您可以使用一些模板框架根据需要安全地填充 html 文档中的字段。
Smartyis one popular framework, here's an example how that works (taken from Smarty's crash course).
Smarty是一种流行的框架,这里有一个它是如何工作的示例(取自 Smarty 的速成课程)。
Template File
模板文件
<html>
<head>
<title>User Info</title>
</head>
<body>
User Information:<p>
Name: {$name}<br>
Address: {$address}<br>
</body>
</html>
Php code that plugs name & address into template file:
将名称和地址插入模板文件的 PHP 代码:
include('Smarty.class.php');
// create object
$smarty = new Smarty;
// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');
// display it
$smarty->display('index.tpl');
Aside from Smarty there's dozens of reasonable choices for templating frameworks to fit your tastes. Some are simple, many have some rather sophisticated features.
除了 Smarty 之外,还有许多合理的模板框架选择可以满足您的口味。有些很简单,许多具有一些相当复杂的功能。
回答by Gumbo
You could also place your HTML outside of the PHP code block:
您还可以将 HTML 放在 PHP 代码块之外:
<?php
// PHP code
?>
<head>
<title>title</title>
<style></style>
</head>
<?php
// further PHP code
?>
回答by mr-sk
Dont forget you also have access to HEREDOC (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)
不要忘记您还可以访问 HEREDOC ( http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)
echo <<< HERE
<head>
<title>title</title>
<style></style>
</head>
HERE;
Also, I would look into something like Smarty templates - and more importantly the MVC design pattern which enforces separating markup from business logic.
此外,我会研究类似 Smarty 模板的东西——更重要的是 MVC 设计模式,它强制将标记与业务逻辑分开。
回答by Scott Evernden
user heredoc syntax
用户 heredoc 语法
echo <<<NO_MORE
<head>
<title>title</title>
<style></style>
</head>
NO_MORE;
or just escape from php output html directly ?> ... <?php
或者直接从 php 输出 html 转义 ?> ... <?php
回答by Ikke
I think neither is the best solution. Echoing html is not very readable. I always put the html outside of the php tags. This way, you get the indentation, and it's more readable too.
我认为两者都不是最好的解决方案。回显 html 的可读性不是很好。我总是把 html 放在 php 标签之外。通过这种方式,您可以获得缩进,并且它也更具可读性。
Something like this:
像这样的东西:
?>
<head>
<title>title</title>
<style></style>
</head>
<?php
And if you want to take it further, you can use a template system to really separate the logic from the presentation.
如果您想更进一步,您可以使用模板系统将逻辑与演示真正分开。
回答by Gordon
Since there is already enough alternatives given, I'd just comment on your second approach. If you want to do it with echo, it is better to use:
由于已经提供了足够的替代方案,我只想评论您的第二种方法。如果你想用 echo 来做,最好使用:
echo "<head>", PHP_EOL,
"<title>title</title>", PHP_EOL
"<style></style", PHP_EOL
"</head>", PHP_EOL;
Using PHP_EOLinstead of a custom constant does not litter the global scope with a superfluous constant and also makes the newline OS-independent, as it will use what is defined for the OS PHP is running on.
使用PHP_EOL而不是自定义常量不会在全局范围内使用多余的常量,并且还使换行符与操作系统无关,因为它将使用为运行 PHP 的操作系统定义的内容。
Using commas instead of concatenation saves you some memory due to PHP not having to concatenate at all, but just flushing whatever you throw at echo.
使用逗号而不是连接可以为您节省一些内存,因为 PHP 根本不需要连接,而只需刷新您在 echo 中抛出的任何内容。
回答by aleXela
This worked fine for me!
这对我来说很好用!
<?php
echo '
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/java.js"></script>
</head>
<body>
<div id="wrap">
<div id="main_home"></div>
<div id="nav_circle"></div>
<input type="text" id="search_text" value="Your search starts here">
</div>
</body>
</html>
';
?>
You can echo large amount of HTML code like this in PHP. You have to be sure that is no ' inside HTML. Replace it with " and it will go on!!
您可以在 PHP 中回显大量这样的 HTML 代码。您必须确保 HTML 中没有 '。用“替换它,它会继续!!
回答by Daan
If you're using quotes to display HTML, use the single quote 'ones because PHP searches for variables into the double quotes ", which is less preferment for showing static strings.
如果您使用引号来显示 HTML,请使用单引号',因为 PHP 在双引号" 中搜索变量,这不太适合显示静态字符串。
For me, the best method remains the one outside the PHP block, some like others answered.
对我来说,最好的方法仍然是 PHP 块之外的方法,有些就像其他人回答的那样。
Also, don't forget the shortcut.
另外,不要忘记快捷方式。
回答by Traveling Tech Guy
The first form is more efficient, as it does not perform string concatenation (.).
第一种形式更有效,因为它不执行字符串连接 ( .)。

