PHP array passing is a value passing rather than a reference passing concept correction

  • 2020-05-27 04:37:15
  • OfStack

Modifying an PHP array in a function does not affect the array itself by assigning it as an argument when the function is called.

The array variable is not a reference to the array itself, the PHP array itself exists as a value, and the parameter is a copy of the array.

This is very different from other languages (c, Js, etc.) and is worth noting!
$arr = array( 
'name' => 'corn', 
'age' => '24', 
function test_arr($arr){ 
$arr['name'] = 'qqyumidi'; 
print_r($arr); //result: Array ( [name] => corn [age] => 24 ) 

The Js code is as follows:
var arr = new Array('corn', '24'); 
function test_arr(arr){ 
arr[0] = 'qqyumidi'; 
console.log(arr); //result:["qqyumidi", "24"] 

Related articles: