Home >Backend Development >PHP Problem >There are several ways to assign values to variables in php
How many ways are there to assign values to variables in php?
3 ways to assign values to variables in php
1. Value assignment, such as $a=1,$b=$a, etc.;
2. Reference assignment, for example $a=&$b, that is, $a and $b both point to the same storage variable value address in memory;
3. Reference counting value transfer, The default value-passing method for objects in php and js is reference counting. The example is as follows:
<?php 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
It can be seen that the object's value-passing method is somewhat different from the second reference-counting method. difference.
Recommended: "PHP Tutorial"
The above is the detailed content of There are several ways to assign values to variables in php. For more information, please follow other related articles on the PHP Chinese website!