Search code examples
phpperformancebyref

Are primitive data types in PHP passed by reference?


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.


Solution

  • 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.

    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.