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

藏色散人
Release: 2023-02-28 13:00:01
Original
5030 people have browsed it

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
Copy after login

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!

Related labels:
php
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