jipskin Posted November 9, 2006 Share Posted November 9, 2006 Hey everyone,I'm coming to PHP from a C++ background and was wondering if PHP automatically passes arguments to a function by reference and if functions return values by reference (where applicable). I am concerned about performance. I have a function that takes in an array as one of its parameters and I'd like to make sure it's passing a pointer to the array (which I assume it is) and I'm also returning arrays of data from functions. These functions will need to be called frequently.Thanks,Josh Quote Link to comment Share on other sites More sharing options...
Orio Posted November 9, 2006 Share Posted November 9, 2006 From the manual:[quote]By default, function arguments are passed by value (so that if you change the value of the argument within the function, it does not get changed outside of the function). If you wish to allow a function to modify its arguments, you must pass them by reference. If you want an argument to a function to always be passed by reference, you can prepend an ampersand (&) to the argument name in the function definition.[/quote]See more [url=http://www.php.net/manual/en/functions.arguments.php]here[/url].Orio. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 10, 2006 Share Posted November 10, 2006 Another tidbit of information to help efficiency.. arguments passed by value are passed copy-on-write. As long as you don't modfy them, the data is shared. But once you modify them, you will trigger copying of any large data structures. That means you don't need to pass large data structures by reference if you don't plan to modify them. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.