输出html与使用echo有什么不同? [英] Is there harm in outputting html vs. using echo?

查看:158
本文介绍了输出html与使用echo有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该怎么说,但我可以证明这一点:

 <?php 
if(true){
echo< h1>内容标题< / h1>;
}
?>

vs

 <?php if(true){?> 
< h1>内容标题< / h1>
<?php}?>

两者有什么不同?会不会有使用回声造成的问题?编写echohtml代码似乎非常繁琐;所有的时间,特别是对于HTML的更大的段。



另外,奖励给可以更好地改写我的问题的人的荣誉。 :)

解决方案



<$如果(真){
回声< h1>内容标题< / h1>; p $ p> <?php

}
?>

在这里,因为您使用双引号,您可以插入变量并使其值呈现。例如:

 <?php 
$ mytitle ='foo';
if(true){
echo< h1> $ mytitle< / h1>;
}
?>

在第二个示例中,您必须在php块中包含回显:

 <?php if(true){?> 
< h1><?php echo'My Title'; ?>< / H1>
<?php}?>

就我个人而言,我使用与第二个例子相同的格式,使用有点歪曲:

 <?php if(true):?> 
< h1><?php echo $ mytitle; ?>< / H1>
<?php endif; ?>

我发现它提高了可读性,特别是当您嵌套控制语句时。


I have no idea really how to say this, but I can demonstrate it:

<?php
if (true) {
echo "<h1>Content Title</h1>";
}
?>

vs

<?php  if (true) {  ?>
<h1>Content Title</h1>
<?php  }  ?>

What differences are there between the two? Will there be problems caused by not using echo? It just seems super tedious to write echo "html code"; all the time, specially for larger segments of html.

Also, bonus kudos to someone who can rephrase my question better. :)

解决方案

There's a small difference between the two cases:

<?php
if (true) {
    echo "<h1>Content Title</h1>";
}
?>

Here, because you're using double quotes in your string, you can insert variables and have their values rendered. For example:

<?php
$mytitle = 'foo';
if (true) {
    echo "<h1>$mytitle</h1>";
}
?>

Whereas in your second example, you'd have to have an echo enclosed in a php block:

<?php  if (true) {  ?>
    <h1><?php echo 'My Title'; ?></h1>
<?php  }  ?>

Personally, I use the same format as your second example, with a bit of a twist:

<?php if (true): ?>
    <h1><?php echo $mytitle; ?></h1>
<?php endif; ?>

I find that it increases readability, especially when you have nested control statements.

这篇关于输出html与使用echo有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆