Demystifying pass-by-value and pass-by-reference in PHP function calls

WBOY
Release: 2024-04-16 14:39:01
Original
1093 people have browsed it

Function calls in PHP can be passed by value or by reference. The default is to pass by value, the function receives a copy of the parameter, and modifications to it do not affect the original value. Pass by reference is declared by adding the & symbol before the parameter, and the function directly modifies the passed variable. Passing by reference is useful when you need a function to modify an external variable, such as an array element.

揭秘 PHP 函数调用中的值传递和引用传递

Demystifying value passing and reference passing in PHP function calls

Value passing

In PHP, by default, function calls Use value passing. This means that when a value is passed to a function as an argument, the function will get a copy of the value. Any changes made to this copy will not affect the original value.

function addValue($number) {
  $number += 10;
}

$value = 10;
addValue($value);
echo $value; // 输出:10
Copy after login

As shown in the above example, when $value is passed to the addValue function, the function gets a copy of the variable. Modifications within the function only affect the copy, not the original variable.

Pass by reference

Sometimes, we need a function to directly modify the original variable. In this case, pass by reference can be used. To declare a reference, add a & symbol before the parameter type declaration.

function addReference(&$number) {
  $number += 10;
}

$value = 10;
addReference($value);
echo $value; // 输出:20
Copy after login

As shown in the above example, the $number parameter of the addReference function is declared as a reference. This means that the function will directly modify the passed variable.

Practical Case

Using passing by reference can simplify certain tasks. For example, we have an array containing user data and need to modify it in a function.

$users = [
  [
    'id' => 1,
    'name' => 'John Doe',
  ],
];

function changeName(&$user, $newName) {
  $user['name'] = $newName;
}

changeName($users[0], 'Jane Doe');
echo $users[0]['name']; // 输出:Jane Doe
Copy after login

By declaring the $user parameter as a reference, the changeName function can directly modify the original element in the array.

Conclusion

Understanding pass-by-value and pass-by-reference in PHP is crucial to writing efficient and maintainable code. By using pass-by-reference, we can avoid unnecessary copying of variables and directly modify variables outside the function.

The above is the detailed content of Demystifying pass-by-value and pass-by-reference in PHP function calls. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template