Home > Article > Backend Development > Is PHP array passing by reference?
PHP array transfer is not by reference, but by value; when calling a function, assigning the PHP array as an actual parameter to the formal parameter and modifying it in the function will not affect the array itself, explaining this process The transfer in is by value, and the array variable is not a reference to the array itself.
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
$arr = array( 'name' => 'corn', 'age' => '24', ); test_arr($arr); function test_arr($arr){ $arr['name'] = 'qqyumidi'; } print_r($arr); //result: Array ( [name] => corn [age] => 24 )js code is as follows:
var arr = new Array('corn', '24'); test_arr(arr); function test_arr(arr){ arr[0] = 'qqyumidi'; } console.log(arr); //result:["qqyumidi", "24"]If you need to use the reference passing effect for value passing in PHP, you can add in front of the formal parameter Take the address character &.
$aa = 100; test_vars($aa); function test_vars(&$aa){ $aa = 200; } print_r($aa); //result: 200Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of Is PHP array passing by reference?. For more information, please follow other related articles on the PHP Chinese website!