Home > Backend Development > PHP Tutorial > PHP数组传递是值传递而非引用传递概念纠正_php技巧

PHP数组传递是值传递而非引用传递概念纠正_php技巧

WBOY
Release: 2016-05-17 09:06:55
Original
869 people have browsed it

在调用函数时通过将PHP数组作为实参赋给形参,在函数中修改,并不会影响到数组本身。

说明此过程中的传递为值传递,数组变量并非是指向此数组本身的引用,PHP数组本身以值的形式存在,同时形参是对数组的拷贝。

这点与其他一些语言不同(如c、Js等)很不相同,值得注意!

复制代码 代码如下:

$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代码如下:
复制代码 代码如下:

var arr = new Array('corn', '24');
test_arr(arr);
function test_arr(arr){
arr[0] = 'qqyumidi';
}
console.log(arr); //result:["qqyumidi", "24"]
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