PHP中的原始数据类型是通过引用传递的吗?

时间:2022-04-05 16:27:46

In PHP, I'm frequently doing lots of string manipulation. Is it alright to split my code into multiple functions, because if primitive types like strings are passed by value I would be significantly affecting performance.

在PHP中,我经常进行大量的字符串操作。将我的代码拆分成多个函数是否可以,因为如果像字符串这样的原始类型按值传递,那么我将显着影响性能。

5 个解决方案

#1


Only objects are passed by reference.

只有对象通过引用传递。

That doesn't mean you'll get a performance boost by changing to references though - PHP uses copy-on-write, so a copy is only made if you modify the variable.

这并不意味着你可以通过更改为引用来提高性能 - PHP使用copy-on-write,因此只有在修改变量时才会生成副本。

Splitting your code into functions won't slow it down from that point of view. There is a small overhead for calling a function, but unless your in a loop calling 10,000s of them it's probably not something you need to worry about.

将代码拆分为函数不会从这个角度减慢速度。调用函数的开销很小,但除非你在一个循环中调用10,000个函数,否则你可能不需要担心。

#2


Objects are passed by reference. Everything else is passed by value unless you explicitly use pass-by-reference with the & operator.

对象通过引用传递。除非您明确使用&运算符传递引用,否则其他所有内容都将按值传递。

That being said, PHP also uses copy-on-write to avoid unnecessary copying.

话虽这么说,PHP也使用copy-on-write来避免不必要的复制。

#3


Yes, primitives are passed by value unless you explicitly define the function to pass by reference (by using an ampersand & in front of the parameter) or invoke the function with an ampersand in front of the argument. (The latter of which is deprecated)

是的,原语按值传递,除非您明确定义要通过引用传递的函数(通过在参数前面使用&符号)或在参数前面调用带符号的函数。 (后者不推荐使用)

See this part of the documentation for more.

有关更多信息,请参阅文档的这一部分。

EDIT

Also, the statement that "objects are passed by reference" in PHP is a bit of a simplification, though it can often be thought of that way for most purposes. This chapter of the documentation explains the differences.

此外,PHP中“通过引用传递对象”的语句有点简化,尽管通常可以为大多数目的考虑这种方式。本文档的这一章解释了这些差异。

#4


Passing by reference is actually slower than passing by value in PHP. I can't find the correct citation for this claim; it's somewhere in the "References" section of the PHP manual.

通过引用传递实际上比在PHP中传递值慢。我找不到这个说法的正确引用;它位于PHP手册的“参考”部分。

#5


By default, everything is passed by value. If you want to pass something by reference you have to explicitly state it as so.

默认情况下,所有内容都按值传递。如果你想通过引用传递一些东西,你必须明确地说明它。

Here is the php documentation that explicitly states this behavior.

这是明确说明此行为的php文档。

#1


Only objects are passed by reference.

只有对象通过引用传递。

That doesn't mean you'll get a performance boost by changing to references though - PHP uses copy-on-write, so a copy is only made if you modify the variable.

这并不意味着你可以通过更改为引用来提高性能 - PHP使用copy-on-write,因此只有在修改变量时才会生成副本。

Splitting your code into functions won't slow it down from that point of view. There is a small overhead for calling a function, but unless your in a loop calling 10,000s of them it's probably not something you need to worry about.

将代码拆分为函数不会从这个角度减慢速度。调用函数的开销很小,但除非你在一个循环中调用10,000个函数,否则你可能不需要担心。

#2


Objects are passed by reference. Everything else is passed by value unless you explicitly use pass-by-reference with the & operator.

对象通过引用传递。除非您明确使用&运算符传递引用,否则其他所有内容都将按值传递。

That being said, PHP also uses copy-on-write to avoid unnecessary copying.

话虽这么说,PHP也使用copy-on-write来避免不必要的复制。

#3


Yes, primitives are passed by value unless you explicitly define the function to pass by reference (by using an ampersand & in front of the parameter) or invoke the function with an ampersand in front of the argument. (The latter of which is deprecated)

是的,原语按值传递,除非您明确定义要通过引用传递的函数(通过在参数前面使用&符号)或在参数前面调用带符号的函数。 (后者不推荐使用)

See this part of the documentation for more.

有关更多信息,请参阅文档的这一部分。

EDIT

Also, the statement that "objects are passed by reference" in PHP is a bit of a simplification, though it can often be thought of that way for most purposes. This chapter of the documentation explains the differences.

此外,PHP中“通过引用传递对象”的语句有点简化,尽管通常可以为大多数目的考虑这种方式。本文档的这一章解释了这些差异。

#4


Passing by reference is actually slower than passing by value in PHP. I can't find the correct citation for this claim; it's somewhere in the "References" section of the PHP manual.

通过引用传递实际上比在PHP中传递值慢。我找不到这个说法的正确引用;它位于PHP手册的“参考”部分。

#5


By default, everything is passed by value. If you want to pass something by reference you have to explicitly state it as so.

默认情况下,所有内容都按值传递。如果你想通过引用传递一些东西,你必须明确地说明它。

Here is the php documentation that explicitly states this behavior.

这是明确说明此行为的php文档。