Is php pass by reference or value?
TL;DR: PHP supports both pass by value and pass by reference. References are declared using an ampersand ( Show For anyone who comes across this in the future, I want to share this gem from the PHP docs, posted by an anonymous user:
And here's the example they provide:
I wrote an extensive, detailed blog post on this subject for JavaScript, but I believe it applies equally well to PHP, C++, and any other language where people seem to be confused about pass by value vs. pass by reference. Clearly, PHP, like C++, is a language that does support pass by reference. By default, objects are passed by value. When working with variables that store objects, it helps to see those variables as pointers (because that is fundamentally what they are, at the assembly level). If you pass a pointer by value, you can still "trace" the pointer and modify the properties of the object being pointed to. What you cannot do is have it point to a different object. Only if you explicitly declare a parameter as being passed by reference will you be able to do that. You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows:
The following things can be passed by reference:
No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid:
tnestved at yahoo dot com ¶ 7 years ago
ccb_bc at hotmail dot com ¶ 3 years ago
$x = 10; mike at eastghost dot com ¶ 7 years ago
nickshanks at nickshanks dot com ¶ 5 years ago
$array; rob at librobert dot net ¶ 10 months ago
Jason Steelman ¶ 2 years ago
phpnet at holodyn dot com ¶ 8 years ago
diabolos @t gmail dot com ¶ 10 years ago
$a = 123.456; yiangforwork at gmail dot com ¶ 2 years ago
tianyiw at vip dot qq dot com ¶ 1 year ago
class problem2 fdelizy at unfreeze dot net ¶ 16 years ago
AddTo( $key, $val, &$array = null) fladnag at zerezo dot com ¶ 5 years ago
no at spam dot please ¶ 7 years ago
Sergio Santana: ssantana at tlaloc dot imta dot mx ¶ 18 years ago
tst($arr0, &$arr1); // We are passing a reference to '$arr1' in the call !
print_r($arr0); // Contains just the reference to $arr1
array_push($arr0, 5); // we add another element to $arr0
pillepop2003 at yahoo dot de ¶ 17 years ago
obscvresovl at NOSPAM dot hotmail dot com ¶ 17 years ago
= 1;
pallsopp at gmail dot com ¶ 5 years ago
Is PHP pass by reference?Introduction. In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.
What is the difference between pass by value and pass by reference in PHP?Definition. Pass by value refers to a mechanism of copying the function parameter value to another variable while the pass by reference refers to a mechanism of passing the actual parameters to the function. Thus, this is the main difference between pass by value and pass by reference.
Are objects in PHP 5 passed by value or reference explain?Objects are passed by reference in PHP 5 and by value in PHP 4. Variables are passed by value by default! "Objects" are not values in PHP5 and cannot be "passed". All variables are passed by value if the parameter of the function passed to does not have & .
Is passing by reference faster PHP?From the PHP Manual itself, “Note that passing by reference doesn't speed up your php script. PHP is smart enough not to simply copy data every time the language requires it.” Currently, the most authoritative guidance I'm aware of on this topic is Schlüter (2010), who literally says, “References in PHP are bad.
|