Home > Backend Development > PHP Tutorial > php中变量的3中赋值方式

php中变量的3中赋值方式

WBOY
Release: 2016-06-20 12:44:48
Original
984 people have browsed it

1、传值赋值,例如$a=1,$b=$a等;

2、引用赋值,例如$a=&$b,即$a和$b在都指向了内存中的同一个存储变量值得地址;

3、引用计数传值,在php和js中的对象都是默认的传值方式都是引用计数传值,例子如下:

class Dog{

    public $name="小花";

    public $leg=4;

}

$a=new Dog;//此时,$a指向了内存中的一个地址(假设0XFFAD[1]),该地址又指向最终对象的值

$b=$a;//此时,$b和$a都指向了内存中的另一个地址(0XFFAD[2]),该地址又指向最终对象的值

var_dump($b->leg);//结果是int 4

$b=999;

var_dump($b);//结果是int 999;

var_dump($a);//此时的结果不是int 999,而是object(Dog)[1]

                                                                    

public 'name' => string '灏��' (length=6)  public 'leg' => int 4
Copy after login

由此可以看出对象的传值方式跟第二种引用传值是有点区别的。

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