Is it efficient to return an array in php?

I am new to PHP; most of my programming experience so far has been in C ++. So naturally, I’m concerned about efficiency. In C ++, you never returned an object or array at the end of a function, but if you need data, you simply return a pointer.

So my question is: is it bad for efficiency to use arrays as return values ​​or is PHP just using a pointer in the background and just not showing me for convenience?

+6
source share
1 answer

PHP returns a link if it is optimal for the interpreter to do this . Usually, the parameters passed to the function are copied, although you pass the parameter by reference and, therefore, get the return value by reference like this:

function blah(&$foo) { $foo = array();//or whatever //note no return statement } //elsewhere $x = "whatever"; blah($x); //$x is now an array. 

Because of & , $foo treated as a link, and therefore modifications to this variable are treated as modifications to the link.

Or you can force the function to return the link:

 function &blah() { //some stuff return $foo;//goes back as a reference } 

This last one should not, according to the documents, be fulfilled unless you have a reason for optimization.

However, PHP is not very efficient, and worrying about these things is usually premature - unless you see the actual bottleneck in your code.

+2
source

Source: https://habr.com/ru/post/974188/


All Articles