在PHP中echo和print有何不同? [英] How are echo and print different in PHP?

查看:66
本文介绍了在PHP中echo和print有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
参考:比较PHP的打印和回显

Possible Duplicate:
Reference: Comparing PHP's print and echo

这两个函数在PHP中是否有主要和基本的区别?

Is there any major and fundamental difference between these two functions in PHP?

推荐答案

来自: http://web.archive.org/web/20090221144611/http://faqts.com/knowledge_base/view.phtml/aid/1/fid/40

  1. 速度.两者之间是有区别的,但是在速度上是有区别的 应该与您使用哪一个无关.回声略快 因为如果您真的想深入到 坚韧不拔.

  1. Speed. There is a difference between the two, but speed-wise it should be irrelevant which one you use. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.

表达. print()的行为类似于一个函数,您可以执行以下操作: $ret = print "Hello World";并且$ret将是1.那意味着打印 可以用作echo无法执行的更复杂表达式的一部分.一个 PHP手册中的示例:

Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

$b ? print "true" : print "false";

print也是优先级表的一部分,如果需要的话, 用于复杂的表达式中.就在底部 优先列表中的.仅, AND OR XOR较低.

print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only , AND OR XOR are lower.

  1. 参数.语法为:echo expression [, expression[, expression] ... ]echo ( expression, expression )无效. 这将是有效的:echo ("howdy"),("partner");等同于:echo "howdy","partner"; (将括号放在该简单示例中 供应 没有目的,因为单个操作符没有运算符优先级问题 这样的术语.)
  1. Parameter(s). The grammar is: echo expression [, expression[, expression] ... ] But echo ( expression, expression ) is not valid. This would be valid: echo ("howdy"),("partner"); the same as: echo "howdy","partner"; (Putting the brackets in that simple example serves no purpose since there is no operator precedence issue with a single term like that.)

因此,不带括号的echo可以采用多个参数,这些参数可以 串联:

So, echo without parentheses can take multiple parameters, which get concatenated:

   echo  "and a ", 1, 2, 3;   // comma-separated without parentheses
   echo ("and a 123");        // just one parameter with parentheses

print()只能采用一个参数:

   print ("and a 123");
   print  "and a 123";

这篇关于在PHP中echo和print有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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