我是PHP新手,我无法弄清楚使用echo函数的规则是什么。例如,如果我需要回显一大块css / js,我是否需要为每行文本添加回声,或者有一种方法可以用一个回声回显大块代码吗?
当我尝试回复像这样的大块代码时,我收到一个错误:
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 {
}
是否有更好的方法可以在没有大量工作的情况下回显大块代码(例如,为每行添加回声)?
答案 0 :(得分:133)
Heredoc syntax非常有用:
// 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;
答案 1 :(得分:63)
一个选择是退出php块并只写HTML。
使用你的代码,在if语句的大括号之后,结束PHP:
if (is_single()) { ?>
然后移除echo '
和';
在所有html和css之后,在结束}
之前,写下:
<? } else {
如果您要写入页面的文本是动态的,它会变得有点棘手,但是现在这应该可以正常工作。
答案 2 :(得分:20)
结帐heredoc。例如:
echo <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo <<<"FOOBAR"
Hello World!
FOOBAR;
也是nowdoc但是在块内部没有进行解析。
echo <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
答案 3 :(得分:6)
回显包含换行符的文本很好,并且一次可以回显的文本或行数没有限制(保存可用内存)。
代码中的错误是由字符串中出现的未转义的单引号引起的。
见这一行:
$('input_6').hint('ex: myname@example.com');
您需要在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 {
}
或者另一种可能性更好的替代方案是将所有静态HTML放入另一个页面并包含()它。
答案 4 :(得分: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!
}
?>
我想知道为什么这么多人坚持丑陋的heredoc。
答案 5 :(得分:5)
您的问题实际上是由以下原因造成的:
$('input_6').hint('ex: myname@example.com');
您需要将单引号转义为\'
然而:使用Heredoc是一个更好的主意,因为它会更整洁。
答案 6 :(得分:2)
要扩展@ 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; ?>
答案 7 :(得分:1)
在您需要的地方突破。
<html>
(html code)
<?php
(php code)
?>
(html code)
</html>
请勿使用缩写形式。 <?
与XML冲突,并且在大多数服务器上默认禁用。
答案 8 :(得分:0)
我更喜欢将多个字符串连接在一起。这适用于echo和变量。 如果您按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>'
;
通常我以空字符串开头然后逐位追加它:
$foo = '';
$foo .= 'function sayHello()'
.' alert( "Hello" );'
."}\n";
$foo .= 'function sum( a , b )'
.'{'
.' return a + b ;'
."}\n";
(请停止帖子喜欢&#34;呃。你回答了一个五个老问题。&#34;为什么不呢?有很多人在寻找答案。使用五年有什么不对旧的想法?如果他们找不到他们的解决方案,他们会打开一个新的问题。然后前五个答案只是&#34;在你问之前使用搜索功能!&#34;所以,我给你另一种解决方案来解决这样的问题。)
答案 9 :(得分:0)
$num = 5;
$location = 'tree';
$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);
答案 10 :(得分:-1)
您可以通过打印字符串来实现:
<?php $string ='here is your string.'; print_r($string); ?>