PHP Echo大块文本

问题描述 投票:40回答:11

我是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: [email protected]');
});
</script>';
}else {

}

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

php html
11个回答
63
投票

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

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

if (is_single()) { ?>

然后删除echo '';

毕竟你的html和css,在结束}之前,写道:

<? } else {

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


0
投票
$num = 5;
$location = 'tree';

$format = 'There are %d monkeys in the %s';
echo sprintf($format, $num, $location);

-1
投票

您可以通过打印字符串来实现:

<?php $string ='here is your string.'; print_r($string); ?>

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;

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;

6
投票

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

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

看到这一行:

$('input_6').hint('ex: [email protected]');

您需要在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: [email protected]');
});
</script>
  <?php
}else {

}

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


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
投票

你的问题实际上是由:

$('input_6').hint('ex: [email protected]');

你需要将单引号转义为\'

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


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; ?>

1
投票

只需突破你需要的地方。

<html>
(html code)
<?php
(php code)
?>
(html code)
</html>

不要使用缩短形式。 <?与XML冲突,在大多数服务器上默认禁用。


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";

(请停止帖子,如“呃。你回答五个老问题。”为什么不呢?有很多人在寻找答案。使用五年之久的想法有什么不对?如果他们找不到“他们的”解决方案他们会打开一个新的问题。然后前五个答案只是“在你问之前使用搜索功能!”所以。我给你另一个解决方案来解决这样的问题。)

© www.soinside.com 2019 - 2024. All rights reserved.