Jump to content

Passing parameters and returning values by reference?


jipskin

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.