Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Building WordPress Widgets, and Shortcodes How to Create WordPress Widgets

Cristian-Adrian Frasineanu
Cristian-Adrian Frasineanu
5,101 Points

Is PHP passing by reference?

I might be confusing with the JavaScript way of only passing objects by reference. So, my question is when we pass the old instance of the object to the local variable is it actually passing by reference? Thanks.

1 Answer

Here's what the PHP documentation has to say:

"By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition:."

http://fr2.php.net/manual/en/functions.arguments.php

Hope this helps.

Cristian-Adrian Frasineanu
Cristian-Adrian Frasineanu
5,101 Points

Alright, so only using the ampersand C++ style reference we can pass by reference, in this case it will create only a copy of that object? Thanks