php : echo"", print(), printf()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1504797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php : echo"", print(), printf()
提问by menardmam
Is there a better way to output data to html page with PHP?
有没有更好的方法用PHP将数据输出到html页面?
If I like to make a div with some var in php, I will write something like that
如果我喜欢在 php 中用 var 做一个 div,我会写这样的东西
print ('<div>'.$var.'</div>');
or
或者
echo "'<div>'.$var.'</div>'";
What is the proper way to do that?
这样做的正确方法是什么?
Or a better way, fill a $tempvarand print it once? like that:
或者更好的方法,填写 a$tempvar并打印一次?像那样:
$tempvar = '<div>'.$var.'</div>'
print ($tempvar);
In fact, in real life, the var will be fill with much more!
事实上,在现实生活中,var 会充满更多!
回答by Asaph
There are 2 differences between echoand printin PHP:
PHPecho和printPHP之间有两个区别:
printreturns a value. It always returns 1.echocan take a comma delimited list of arguments to output.
print返回一个值。它总是返回 1。echo可以采用逗号分隔的参数列表来输出。
Always returning 1 doesn't seem particularly useful. And a comma delimited list of arguments can be simulated with multiple calls or string concatenation. So the choice between echoand printpretty much comes down to style. Most PHP code that I've seen uses echo.
总是返回 1 似乎不是特别有用。并且可以使用多个调用或字符串连接来模拟逗号分隔的参数列表。所以在echo和之间的选择print几乎归结为风格。我见过的大多数 PHP 代码都使用echo.
printf()is a direct analog of c's printf(). If you're comfortable in the c idiom, you might use printf(). A lot of people in the younger generation though, find printf()'s special character syntax to be less readable than the equivalent echocode.
printf()是 c's 的直接模拟printf()。如果您习惯使用 c 习语,则可以使用printf(). 但是,年轻一代中的很多人发现printf()的特殊字符语法比等效echo代码可读性差。
There are probably differences in performance between echo, printand printf, but I wouldn't get too hung up on them since in a database driven web application (PHP's typical domain), printing strings to the client is almost certainly not your bottleneck. The bottom line is that any of the 3 will get the job done and one is not better than another. It's just a matter of style.
echo,print和之间的性能可能存在差异printf,但我不会太在意它们,因为在数据库驱动的 Web 应用程序(PHP 的典型域)中,向客户端打印字符串几乎肯定不是您的瓶颈。最重要的是,这 3 个中的任何一个都可以完成工作,并且一个并不比另一个好。这只是风格问题。
回答by Atmocreations
you can even write
你甚至可以写
$var = "hello";
echo "Some Text $var some other text";
// output:
// Some Text hello some other text
or
或者
print("Some Text $var some other text");
// output:
// Some Text hello some other text
doesn't make big difference. This works with double-quotes only. With single quotes it doesn't. example:
没有太大区别。这仅适用于双引号。使用单引号则不然。例子:
$var = "hello";
echo 'Some Text $var some other text'; // Note the single quotes!
// output:
// Some Text $var some other text
or
或者
print('Some Text $var some other text'); // Note the single quotes!
// output:
// Some Text $var some other text
回答by JF Simon
Just try this you gonna love the well formated amount of infos :
试试这个,你会喜欢格式良好的信息量:
<?php
echo '<pre>';
var_dump($your_var);
echo '</pre>';
?>
OK, I explain : set a "code" html format and var_dump show the value, the type, the params ... of the variable.
好的,我解释一下:设置“代码”html 格式,var_dump 显示变量的值、类型、参数...。
回答by Anax
You can also use the following syntax:
您还可以使用以下语法:
echo <<<ENDOFTEXT
<div>
$var
</div>
ENDOFTEXT;
Just make sure the ENDOFTEXT is not indented.
只要确保ENDOFTEXT没有缩进。
回答by Will Shaver
<div><? print($var); ?></div>
Or if you don't have short tags on, you might need to
或者,如果您没有短标签,则可能需要
<div><?php print($var); ?></div>
if you have the short_open_tag option enabled you can do
如果您启用了 short_open_tag 选项,则可以执行
<?=$var?>
But some find that messy.
但有些人觉得这很乱。
回答by Gumbo
While echoand printare almost equal, you a using different values. Your first value will result in
虽然echo和print几乎相等,但您使用不同的值。您的第一个值将导致
<div><value of $var><div>
while the second will result in
而第二个将导致
'<div>'.<value of $var>.'<div>'
But the rest is semantically almost equal. Since echoand printare no real functions but special language constructs, the parenthesis in your first example is just wrapping the single string value and not the parameter list.
但其余的在语义上几乎相同。由于echo和print不是真正的函数而是特殊的语言结构,因此第一个示例中的括号只是包装单个字符串值而不是参数列表。
See also https://stackoverflow.com/questions/1462581#1462636and https://stackoverflow.com/questions/1163473#1163793.
另见https://stackoverflow.com/questions/1462581#1462636和https://stackoverflow.com/questions/1163473#1163793。
回答by Doctor Blue
You could do something like this:
你可以这样做:
<div><?php echo $var; ?></div>
One of the nice things about PHP is that you can insert it into regular HTML, and accomplish things like the above with ease. I've always used echo myself in PHP. Not sure if it's the "proper" way, but it's the easiest.
PHP 的优点之一是您可以将其插入到常规 HTML 中,并轻松完成上述操作。我一直在 PHP 中使用 echo 我自己。不确定这是否是“正确”的方式,但这是最简单的。
回答by redben
I have read somewhere that echo is faster that print. But its just too small of a performance gain.
我在某处读到 echo 打印速度更快。但它的性能增益太小了。

