PHP中=赋值操作符对不同数据类型的不同行为_php技巧
首先解释赋值操作符=的行为,看下面的例子:
$i = 0;
$j = $i;
$j = 0;
echo $j; // 打印输出0
$arr = array(0);
$arr2 = $arr;
$arr2[0] = 1;
echo $arr[0]; //打印输出0
class B
{
public $i = 0;
}
$b = new B();
$c = $b;
$c->i = 1;
echo($b->i); // 打印输出1
从这个例子可以看出,如果=操作符右边的变量为基本数据类型或者数组,那么=操作符把右边变量的一份拷贝赋值给左边变量;如果右边变量不是基本数据类型或者数组,如class,那么=会把一个指向右边变量的引用赋值给左边变量。注意:是指向右边变量的引用,而不是指向右边变量所指的内容区域的引用;具体看下边的例子
$a = new A();
$b_a = $a;
$b_r = &$a;
$b_a = null;
var_dump($a); //打印 object(A)[2],$a所指向的内容还在
$b_r = null;
var_dump($a); // 打印 null,$a所指向的内容被清除了
上面的例子也说明了,如果用 $var = &$a 的方式赋值的话,用$var=null来销毁变量$var的话事实上是把$var所指内容被设置null了,其实这句话也暗示了任何一个指向该内容区域的引用变量均可用来销毁该内容区域的内容。所以,要销毁变量$var的话用 unset($var) 。PS:事实上一这种方式赋值$var只是个引用,占用不了多少内存,要不要销毁没所谓,这里这是说下必须用unset的方式销毁。
下面则是《用户手册》中的“引用的解释”的例子:
$a =& $b;
下边有这么一句解释:
这意味着 $a 和 $b 指向了同一个变量。
注: $a 和 $b 在这里是完全相同的,这并不是 $a 指向了 $b 或者相反,而是 $a 和 $b 指向了同一个地方。
引用是什么?
在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字。最接近的比喻是 Unix 的文件名和文件本身――变量名是目录条目,而变量内容则是文件本身。引用可以被看作是 Unix 文件系统中的紧密连接。
关于“引用是什么”的一点解释:
int i = 0;
int j = 0;
int *p = &i;
p = &j;
上面的代码中,p是一个指向i的内存地址的指针,而*p才是其中的内容;p=&j指向改变了p指针的指向,用*p=111的表达式才会改变i的内容。而PHP中则不是,下面的例子
$i = 0;
$p = &$i;
$p = 111则马上会改变$i的值。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
