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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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