Home >Backend Development >PHP Problem >There are several ways to assign values ​​to variables in php

There are several ways to assign values ​​to variables in php

藏色散人
藏色散人Original
2019-11-09 10:31:485022browse

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 &#39;name&#39; => string &#39;灏忚姳&#39; (length=6)  public &#39;leg&#39; => 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!

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
Previous article:How to use php delimitersNext article:How to use php delimiters