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

PHP摘引的理解

WBOY
WBOYOriginal
2016-06-13 13:03:20980browse

PHP引用的理解

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

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

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

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());?

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

Statement:
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