PHP 回显一大块文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2672642/
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
PHP Echo a large block of text
提问by Thomas
Im new to PHP and I can't figure out what the rules are for using the echo function. For example, if I need to echo a large block of css/js, do I need to add echo to each line of text or is there a way to echo a large block of code with a single echo?
我是 PHP 新手,我无法弄清楚使用 echo 函数的规则是什么。例如,如果我需要回显一大块 css/js,我是否需要在每行文本中添加回显,或者有没有办法用单个回显来回显一大块代码?
When I try to echo a big block of code like this one, I get an error:
当我尝试回显像这样的一大块代码时,我收到一个错误:
if (is_single()) {
echo '<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/><style type="text/css">
.form-label{
width:150px !important;
}
.form-label-left{
width:150px !important;
}
.form-line{
padding:10px;
}
.form-label-right{
width:150px !important;
}
body, html{
margin:0;
padding:0;
background:false;
}
.form-all{
margin:0px auto;
padding-top:20px;
width:650px !important;
color:Black;
font-family:Verdana;
font-size:12px;
}
</style>
<link href="http://jotform.com/css/calendarview.css" rel="stylesheet" type="text/css" />
<script src="http://jotform.com/js/prototype.js" type="text/javascript"></script>
<script src="http://jotform.com/js/protoplus.js" type="text/javascript"></script>
<script src="http://jotform.com/js/protoplus-ui.js" type="text/javascript"></script>
<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script>
<script src="http://jotform.com/js/location.js" type="text/javascript"></script>
<script src="http://jotform.com/js/calendarview.js" type="text/javascript"></script>
<script type="text/javascript">
JotForm.init(function(){
$('input_6').hint('ex: [email protected]');
});
</script>';
}else {
}
Is there a better way to echo large blocks of code without a lot of work (adding echo to each line for example)?
有没有更好的方法来回显大块代码而无需大量工作(例如向每一行添加回显)?
回答by nickf
Heredoc syntaxcan be very useful:
Heredoc 语法非常有用:
// start the string with 3 <'s and then a word
// it doesn't have to be any particular string or length
// but it's common to make it in all caps.
echo <<< EOT
in here is your string
it has the same variable substitution rules
as a double quoted string.
when you end it, put the indicator word at the
start of the line (no spaces before it)
and put a semicolon after it
EOT;
回答by hookedonwinter
One option is to get out of the php block and just write HTML.
一种选择是摆脱 php 块并只编写 HTML。
With your code, after the opening curly brace of your if statement, end the PHP:
使用您的代码,在 if 语句的左花括号之后,结束 PHP:
if (is_single()) { ?>
Then remove the echo 'and the ';
然后删除echo '和';
After all your html and css, before the closing }, write:
毕竟你的 html 和 css,在结束之前},写:
<? } else {
If the text you want to write to the page is dynamic, it gets a little trickier, but for now this should work fine.
如果您想写入页面的文本是动态的,它会变得有点棘手,但现在这应该可以正常工作。
回答by Galen
Check out heredoc. Example:
查看heredoc。例子:
echo <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo <<<"FOOBAR"
Hello World!
FOOBAR;
The is also nowdocbut no parsing is done inside the block.
也是nowdoc但块内没有进行解析。
echo <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
回答by thomasrutter
Echoing text that contains line breaks is fine, and there's no limit on the amount of text or lines you can echo at once (save for available memory).
回显包含换行符的文本很好,并且您可以一次回显的文本或行数没有限制(节省可用内存)。
The error in your code is caused by the unescaped single quotes which appear in the string.
代码中的错误是由字符串中出现的未转义单引号引起的。
See this line:
看到这一行:
$('input_6').hint('ex: [email protected]');
You'd need to escape those single quotes in a PHP string whether it's a single line or not.
无论是否为单行,您都需要转义 PHP 字符串中的那些单引号。
There is another good way to echo large strings, though, and that's to close the PHP block and open it again later:
不过,还有另一种回显大字符串的好方法,那就是关闭 PHP 块并稍后再次打开它:
if (is_single()) {
?>
<link type="text/css" rel="stylesheet" href="http://jotform.com/css/styles/form.css"/><style type="text/css">
.form-label{
width:150px !important;
}
.form-label-left{
width:150px !important;
}
.form-line{
padding:10px;
}
.form-label-right{
width:150px !important;
}
body, html{
margin:0;
padding:0;
background:false;
}
.form-all{
margin:0px auto;
padding-top:20px;
width:650px !important;
color:Black;
font-family:Verdana;
font-size:12px;
}
</style>
<link href="http://jotform.com/css/calendarview.css" rel="stylesheet" type="text/css" />
<script src="http://jotform.com/js/prototype.js" type="text/javascript"></script>
<script src="http://jotform.com/js/protoplus.js" type="text/javascript"></script>
<script src="http://jotform.com/js/protoplus-ui.js" type="text/javascript"></script>
<script src="http://jotform.com/js/jotform.js?v3" type="text/javascript"></script>
<script src="http://jotform.com/js/location.js" type="text/javascript"></script>
<script src="http://jotform.com/js/calendarview.js" type="text/javascript"></script>
<script type="text/javascript">
JotForm.init(function(){
$('input_6').hint('ex: [email protected]');
});
</script>
<?php
}else {
}
Or another alternative, which is probably better for readability, is to put all that static HTML into another page and include() it.
或者另一种可能更利于可读性的替代方法是将所有静态 HTML 放入另一个页面并 include() 它。
回答by Your Common Sense
Man, PHP is not perl!
PHP can just escape from HTML :)
http://www.php.net/manual/en/language.basic-syntax.phpmode.php
伙计,PHP 不是 perl!
PHP 可以摆脱 HTML :)
http://www.php.net/manual/en/language.basic-syntax.phpmode.php
if (is_single()) {
//now we just close PHP tag
?>
</style>
<script>
<blah blah blah>
<?php
//open it back. here is your PHP again. easy!
}
?>
I wonder why such many people stuck to ugly heredoc.
我想知道为什么这么多人坚持丑陋的heredoc。
回答by Mitch Dempsey
Your problem is actually caused by:
您的问题实际上是由以下原因引起的:
$('input_6').hint('ex: [email protected]');
You need to escape the single quotes to be \'
您需要将单引号转义为 \'
However:Using a Heredoc is a much better idea, as it will be much cleaner overall.
但是:使用 Heredoc 是一个更好的主意,因为它总体上会更干净。
回答by moteutsch
To expand on @hookedonwinter's answer, here's an alternate (cleaner, in my opinion) syntax:
为了扩展@hookedonwinter 的回答,这里有一个替代的(在我看来更干净)语法:
<?php if (is_single()): ?>
<p>This will be shown if "is_single()" is true.</p>
<?php else: ?>
<p>This will be shown otherwise.</p>
<?php endif; ?>
回答by user186658
Just break out where you need to.
只需在需要的地方突破即可。
<html>
(html code)
<?php
(php code)
?>
(html code)
</html>
Do not use shortened-form.<?conflicts with XML and is disabled by default on most servers.
不要使用缩写形式。<?与 XML 冲突,并且在大多数服务器上默认禁用。
回答by hiddenAlpha
I prefer to concatenate multiple Strings together. This works either for echo AND for variables. Also some IDEs auto-initialize new lines if you hit enter. This Syntax also generate small output because there are much less whitespaces in the strings.
我更喜欢将多个字符串连接在一起。这既适用于 echo AND 也适用于变量。如果您按 Enter,一些 IDE 还会自动初始化新行。此语法还会生成较小的输出,因为字符串中的空格要少得多。
echo ''
.'one {'
.' color: red;'
.'}'
;
$foo = ''
.'<h1>' . $bar . '</h1>' // insert value of bar
.$bar // insert value of bar again
."<p>$bar</p>" // and again
."<p>You can also use Double-Quoted \t Strings for single lines. \n To use Escape Sequences.</p>"
// also you can insert comments in middle, which aren't in the string.
.'<p>Or to insert Escape Sequences in middle '."\n".' of a string</p>'
;
Normally i start with an empty string and then append bit by bit to it:
通常我从一个空字符串开始,然后一点一点地附加到它:
$foo = '';
$foo .= 'function sayHello()'
.' alert( "Hello" );'
."}\n";
$foo .= 'function sum( a , b )'
.'{'
.' return a + b ;'
."}\n";
(Please stop Posts like "uh. You answer to an five jears old Question." Why not? There are much people searching for an answer. And what's wrong to use five year old ideas? If they don't find "their" solution they would open a new Question. Then the first five answers are only "use the search function before you ask!" So. I give you another solution to solve problems like this.)
(请停止类似“呃。你回答了一个五年前的问题。”为什么不?有很多人在寻找答案。使用五年前的想法有什么错?如果他们没有找到“他们的”解决方案他们会打开一个新问题。然后前五个答案只是“在问之前使用搜索功能!”所以。我给你另一个解决方案来解决这样的问题。)
回答by u1362399
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);

