PHP中返回、echo和print关键字之间的差异

时间:2022-04-12 00:28:17

What is the difference between these three?

这三者的区别是什么?

return , echo and print keyword in PHP

在PHP中返回、echo和打印关键字

function theBand($abc,$bac) {

return $abc;
echo $abc;

}

Both does the same, it does show or return me the value holding in the variable abc. Now return exists the function and echo continues. Apart from this is there anything specific on return keyword.

两者都是一样的,它显示或返回变量abc中的值。现在返回函数和echo继续。除此之外,还有一些特定的返回关键字。

3 个解决方案

#1


15  

return is a language construct used to exit a function and give a value to the caller of the function.

return是用于退出函数的语言构造,并为函数的调用者提供一个值。

echo and print are both language constructs that output strings. The main difference is that echo can take multiple arguments separated by commas, but print accepts only a single argument.

echo和print都是输出字符串的语言结构。主要的区别是echo可以使用由逗号分隔的多个参数,而print只接受一个参数。

#2


3  

return is used when a function has to return a value.

当函数必须返回值时,将使用return。

please see HERE

请在这里看到

echo and print are very similar however echo is faster as it does not return a value.

echo和print非常相似,但是echo更快,因为它不返回值。

  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.

    速度。两者之间是有区别的,但就速度而言,使用哪一种应该无关紧要。echo的速度稍快一些,因为如果你真的想要深入细节的话,它不会设置返回值。

  2. Expression. print() behaves like a function in that you can do:

    表达式。print()的行为类似于一个函数,您可以这样做:

    $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:

    $ret将是1。这意味着打印可以作为一个更复杂的表达式的一部分使用,而echo不能。一个来自PHP手册的示例:

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

    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 and XOR are lower.

    如果要在一个复杂的表达式中使用print,那么它也是优先级表的一部分。不过,它只是在优先级列表的底部。只有","和,或和x较低。

  3. 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.)

    参数(s)。语法是:echo expression[, expression[, expression]…]。但是echo(表达式,表达式)是无效的。这将是有效的:echo(“howdy”)、(“partner”);同:echo "howdy","partner";(将括号放在那个简单示例中没有任何意义,因为这样的单个术语没有优先级问题。)

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

因此,没有括号的echo可以取多个参数,并将它们连接起来:

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

print() can only take one parameter:

print()只能接受一个参数:

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

#3


0  

print returns 1, while echo returns nothing. Echo also can take multiple arguments, as Mark points out.

print返回1,而echo没有返回任何内容。正如马克指出的,Echo也可以使用多个参数。

return (in the context of a function) exits the function (Returning a value, if provided). In the global context, return will stop executing whatever file it's in. So you can bail out of an include file, or halt execution of the main script this way.

return(在函数的上下文中)退出函数(如果提供的话,返回一个值)。在全局上下文中,return将停止执行它所包含的任何文件。因此,您可以从包含文件中退出,或者以这种方式停止执行主脚本。

#1


15  

return is a language construct used to exit a function and give a value to the caller of the function.

return是用于退出函数的语言构造,并为函数的调用者提供一个值。

echo and print are both language constructs that output strings. The main difference is that echo can take multiple arguments separated by commas, but print accepts only a single argument.

echo和print都是输出字符串的语言结构。主要的区别是echo可以使用由逗号分隔的多个参数,而print只接受一个参数。

#2


3  

return is used when a function has to return a value.

当函数必须返回值时,将使用return。

please see HERE

请在这里看到

echo and print are very similar however echo is faster as it does not return a value.

echo和print非常相似,但是echo更快,因为它不返回值。

  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.

    速度。两者之间是有区别的,但就速度而言,使用哪一种应该无关紧要。echo的速度稍快一些,因为如果你真的想要深入细节的话,它不会设置返回值。

  2. Expression. print() behaves like a function in that you can do:

    表达式。print()的行为类似于一个函数,您可以这样做:

    $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:

    $ret将是1。这意味着打印可以作为一个更复杂的表达式的一部分使用,而echo不能。一个来自PHP手册的示例:

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

    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 and XOR are lower.

    如果要在一个复杂的表达式中使用print,那么它也是优先级表的一部分。不过,它只是在优先级列表的底部。只有","和,或和x较低。

  3. 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.)

    参数(s)。语法是:echo expression[, expression[, expression]…]。但是echo(表达式,表达式)是无效的。这将是有效的:echo(“howdy”)、(“partner”);同:echo "howdy","partner";(将括号放在那个简单示例中没有任何意义,因为这样的单个术语没有优先级问题。)

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

因此,没有括号的echo可以取多个参数,并将它们连接起来:

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

print() can only take one parameter:

print()只能接受一个参数:

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

#3


0  

print returns 1, while echo returns nothing. Echo also can take multiple arguments, as Mark points out.

print返回1,而echo没有返回任何内容。正如马克指出的,Echo也可以使用多个参数。

return (in the context of a function) exits the function (Returning a value, if provided). In the global context, return will stop executing whatever file it's in. So you can bail out of an include file, or halt execution of the main script this way.

return(在函数的上下文中)退出函数(如果提供的话,返回一个值)。在全局上下文中,return将停止执行它所包含的任何文件。因此,您可以从包含文件中退出,或者以这种方式停止执行主脚本。