2013-05-26 98 views
1

从tuxradar.com:PHP输出文本WITHOUT echo/print?

实施例1:

<?php 
    if ($foo == $bar) { 
     print "Lots of stuff here"; 
     print "Lots of stuff here"; 
     print "Lots of stuff here"; 
     ...[snip]... 
     print "Lots of stuff here"; 
     print "Lots of stuff here"; 
    } 
?> 

实施例2:

<?php 
    if ($foo == $bar) { 
?> 
    Lots of stuff here 
    Lots of stuff here 
    Lots of stuff here 
    ...[snip]... 
    Lots of stuff here 
    Lots of stuff here 
<?php 
    } 
?> 

假设$foo = $bar

两者的输出相等。我不明白为什么。示例2没有print/echo,据我所知,不应使用printecho的PHP解析器来理解一堆单词。那么为什么当一堆“病房”被另外一组<?php ?>标签分开时,它实际上会被打印?

我想我错过了一些我想了解的核心。

回答

1

示例2输出文本的原因是因为您已关闭PHP标记。浏览器将其渲染/解释为纯文本。您也可以使用HTML格式化文本输出,并且它将在浏览器中很好地呈现。

+2

@frrlod:这是一个非常普遍的做法,可以导致_very漂亮的代码。如果你需要在纯HTML中输出变量,可以考虑这样做:'<?= $ variable?>'或'<?php echo $ variable; ?>'。这都是关于可读性的。 –

+1

@Kristy服务器将其解释为纯文本。反正浏览器不会读PHP。 – Anujan