PHP Echo大块文本

Tho*_*mas 41 html php

我是PHP的新手,我无法弄清楚使用echo函数的规则是什么.例如,如果我需要回显一大块css/js,我是否需要在每行文本中添加echo,或者有一种方法可以用一个回声回显大块代码吗?

当我尝试回显像这样的大块代码时,我收到一个错误:

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: myname@example.com');
});
</script>';
}else {

}
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法来回应大块代码而无需大量工作(例如,为每行添加回声)?

nic*_*ckf 133

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;
Run Code Online (Sandbox Code Playgroud)

  • 我不是heredoc语法的粉丝,但我给你+1,因为你是第一个真正说明heredoc语法在这里的人,这使它成为一个有用的答案. (8认同)
  • 对我不起作用,我得到这个错误:`解析错误:语法错误,意外结束文件,期望变量(T_VARIABLE)或heredoc结束(T_END_HEREDOC)或$ {(T_DOLLAR_OPEN_CURLY_BRACES)或{$(T_CURLY_OPEN)在C:\第256行的xampp\htdocs\mbcl\test.php`编辑:解决了,`EOT`必须在行的开头,不允许任何意图,行中唯一允许的其他字符是`EOT后的semikolon . (3认同)

hoo*_*ter 65

一种选择是退出php块并只写HTML.

使用您的代码,在if语句的大括号之后,结束PHP:

if (is_single()) { ?>
Run Code Online (Sandbox Code Playgroud)

然后删除echo '';

在你所有的html和css之后,在结束之前}写下:

<? } else {
Run Code Online (Sandbox Code Playgroud)

如果您要写入页面的文本是动态的,它会变得有点棘手,但是现在这应该可以正常工作.

  • -1这使代码难以阅读并容易出错,应该避免使用.@nickf提到的Heredoc语法是要走的路. (6认同)
  • 但这是一个可能的解决方案,为什么-1? (4认同)
  • 解决方案是一种解决方案. (4认同)
  • 我觉得这里有很多,太多了,当涉及到downvote按钮时,有点触发快乐. (2认同)
  • 我白天是ASP.NET的人,晚上是PHP的长期人..多年来,由于.NET接管了我的生活,这些年来有些生疏。.完全忘记了我只能直接编写HTML来输出流..在我的index.php中根本没有任何PHP标记..因为需要SO提醒我而感到愚蠢。我的投票是最简单的答案。 (2认同)

Gal*_*len 20

看看heredoc.例:

echo <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

echo <<<"FOOBAR"
Hello World!
FOOBAR;
Run Code Online (Sandbox Code Playgroud)

也是nowdoc但是在块内部没有进行解析.

echo <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
Run Code Online (Sandbox Code Playgroud)


tho*_*ter 7

回显包含换行符的文本很好,并且可以立即回显的文本或行数量没有限制(保存可用内存).

代码中的错误是由字符串中出现的未转义单引号引起的.

看到这一行:

$('input_6').hint('ex: myname@example.com');
Run Code Online (Sandbox Code Playgroud)

您需要在PHP字符串中转义那些单引号,无论它是否为单行.

但是,还有另一种回声大字符串的好方法,那就是关闭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: myname@example.com');
});
</script>
  <?php
}else {

}
Run Code Online (Sandbox Code Playgroud)

或者另一种可能性更好的替代方法是将所有静态HTML放入另一个页面并包含()它.


You*_*nse 6

伙计,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!
}
?>
Run Code Online (Sandbox Code Playgroud)

我想知道为什么这么多人坚持丑陋的heredoc.

  • Heredoc不是那么丑陋,如果你想创建一个字符串和/或插入很多变量,它就很有用.否则,PHP转义是一个更清洁的解决方案.但我觉得我很高兴Perl做不到. (2认同)

Mit*_*sey 5

你的问题实际上是由:

$('input_6').hint('ex: myname@example.com');
Run Code Online (Sandbox Code Playgroud)

你需要逃避单引号 \'

然而:使用Heredoc是一个更好的主意,因为它总体来说会更清晰.