回声网络
PHP is often used to generate small “snippets” of content, often through the echo
function. Just where and how echo
is used is a matter of both efficiency and (to a lesser degree) personal style.
PHP通常用于通过echo
功能生成小的“片段”内容。 在何处以及如何使用 echo
是效率和(在较小程度上)个人风格的问题。
One option is to echo both the content and the HTML surrounding it:
一种选择是同时显示内容和周围HTML:
<?php echo '<p>Some dynamic content here</p>' ?>
…or you could place the HTML code outside the PHP, which is usually my preferred solution, especially when combined with a PHP shortcut for echo
:
…或者您可以将HTML代码放置在PHP 之外 ,这通常是我的首选解决方案,尤其是当与echo
PHP快捷方式结合使用时:
<p><?='Some dynamic content here'?></p>
Neither approach is “wrong”: your chosen solution depends on which one is more efficient.
两种方法都不是“错误的”:您选择的解决方案取决于哪种方法更有效。
There is one final consideration we might make. PHP runs without any awareness of the HTML code before or after it; because of that, the code it generates, while perfectly valid, may not be formatted terribly well when you use "View Source" in your browser. If this is important to you, use "escape characters" in your code:
我们可能要作最后的考虑。 PHP在运行之前或之后都不会对HTML代码有任何了解; 因此,当您在浏览器中使用“查看源代码”时,它生成的代码虽然完全有效,但可能无法很好地格式化。 如果这对您很重要,请在代码中使用“转义符”:
<?php echo '<p>Test content here</p>\n' ?>
That \n
forces PHP to create a new line after it executes, making your code more readable. Escapes may also be used before characters that might otherwise be interpreted incorrectly: for example, if in the above line, it we wanted to say “Test text's here” we would alter our PHP code to:
\n
强制PHP执行后创建新行,使您的代码更具可读性。 还可以在可能会被错误解释的字符之前使用转义符:例如,如果在上一行中,我们想要说“ Test text's here”,则可以将我们PHP代码更改为:
<?php echo '<p>Test text\'s here</p>\n' ?>
In this case the backwards slash character tells PHP that that apostrophe is “real” and is not a single quote that will terminate our code prematurely.
在这种情况下,反斜杠字符告诉PHP撇号是“真实的”,并且不是单引号,它将导致我们的代码过早终止。
Interweaving PHP, HTML and CSS is a very powerful technique, but you must be clear on where each begins and ends in order to produce a whole, complete and valid page.
将PHP , HTML和CSS交织是一种非常强大的技术,但是您必须清楚地知道它们的开始和结束位置,以便生成一个完整,完整和有效的页面。
回声网络