Home > Backend Development > PHP Tutorial > PHP摘引的理解

PHP摘引的理解

WBOY
Release: 2016-06-13 11:01:42
Original
1130 people have browsed it

PHP引用的理解

在PHP中,函数的参数传递默认是值传递,我们可以改成引用传递,只要在定义函数时,在参数前面加一个&就可以,如

    $a = "测试";        function setName(& $name){            $name = "测试OK";       }       setName($a);       echo $a;  //输出 测试OK
Copy after login

?以上只是最普通的参数引用传递,还有一种就是返回值为一个引用,看以下例子

class Test{public $a = array(1,2,3,4);	        function &getA()    {        return $this->a;    }}$a = new Test();$b = &$a->getA();	$b[0] = 101;print_r($a->getA());?
Copy after login

输出内容是: Array ( [0] => 101 [1] => 2 [2] => 3 [3] => 4 )

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