我是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)
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)
如果您要写入页面的文本是动态的,它会变得有点棘手,但是现在这应该可以正常工作.
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)
回显包含换行符的文本很好,并且可以立即回显的文本或行数量没有限制(保存可用内存).
代码中的错误是由字符串中出现的未转义单引号引起的.
看到这一行:
$('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放入另一个页面并包含()它.
伙计,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.
你的问题实际上是由:
$('input_6').hint('ex: myname@example.com');
Run Code Online (Sandbox Code Playgroud)
你需要逃避单引号 \'
然而:使用Heredoc是一个更好的主意,因为它总体来说会更清晰.
归档时间: |
|
查看次数: |
129364 次 |
最近记录: |