Home Backend Development PHP Tutorial PHP4跟PHP5中的引用&

PHP4跟PHP5中的引用&

Jun 13, 2016 pm 01:14 PM
bing echo php5 user

PHP4和PHP5中的引用&
文章转载自重庆PHP,原文地址:http://www.php-chongqing.com/index.php/article/104
PHP提供了引用操作符(&),在PHP中引用的意思是用不同的名字访问同一个变量内容,PHP4和PHP5中的对象引用是有区别的。

$a = 8;
$b = $a;
echo '$a:' . $a; 	// 8
echo '$b:' . $b;	// 8 
$a = 12;
echo '$a:' . $a; 	// 12
echo '$b:' . $b;	// 8 
Copy after login

首先创建变量$a,把整数8赋值给$a,然后把$a赋给$b,这时候PHP创建一个$a的副本,再把这个副本赋给$b,通俗点说就是内存中产生了两个变量,变量的值都是8,这两个变量一个指向$a,一个指向$b,所以打印结果$a和$b都是12,接着我们将12赋给$a,然后再打印$a和$b,可以看到$a的值变成12了,而$b的值还是8,这个很容易理解,因为$a和$b是两个完全不相干的变量。


$a = 8;
$b &= $a;
echo '$a:' . $a; 	// 8
echo '$b:' . $b;	// 8 
$a = 12;
echo '$a:' . $a; 	// 12
echo '$b:' . $b;	// 12
Copy after login

首先创建变量$a,把整数8赋值给$a,然后把$a赋给$b,注意是使用的&引用赋值,打印$a和$b,两个变量的均为8;接着我们把12赋给$a,再打印$a和$b,可以看到$b的值也变成12了。PHP的引用操作符(&),可以不让程序产生一个副本,$a和$b指向同一块内存区域,即$a和$b是同一个变量,所以当$a的值发生变化时,$b的值也会跟着发生变化。这就好比‘张三’,上学的时候同学们给取个绰号‘三娃’,无论是‘张三’还是‘三娃’所指的都是那一百多斤,都是指的同一个人。

在PHP4中,当你创建对象并将其赋值给其它变量时,PHP4总是会产生一个该对象的副本,将整个对象及所有内容复制过去。PHP4的这种对象处理方式非常的糟糕,复制对象会让程序占用更多的内存,复制对象往往还会引起一些莫名其妙的错误。这种糟糕的对象处理方式在PHP5中得到了改善,在PHP5中对象总是以引用的方式传递。
$user = new User();
$user->name = 'zhangsan';
$bing = $user;
$bing->name = 'bing.peng';
echo $user->name; 	// For PHP4: zhangsan	For PHP5: bing.peng
Copy after login

在PHP4下运行上面的代码,打印出来的是:‘zhangsan’;在PHP5下打印结果为:‘bing.peng’,可以看到PHP5默认就是使用引用赋值,注意PHP5只是对象类型默认使用引用赋值,基本类型还是会产生副本,那么要在PHP5中复制对象,需要使用clone关键字来完成,如果想要在PHP4中得到同样的效果,我们就需要使用引用操作符,代码如下:
$user = new User();
$user->name = 'zhangsan';
$bing &= $user;
$bing->name = 'bing.peng';
echo $user->name; 
Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Microsoft bing international version entrance address (bing search engine entrance) Microsoft bing international version entrance address (bing search engine entrance) Mar 14, 2024 pm 01:37 PM

Bing is an online search engine launched by Microsoft. The search function is very powerful and has two entrances: the domestic version and the international version. Where are the entrances to these two versions? How to access the international version? Let’s take a look at the details below. Bing Chinese version website entrance: https://cn.bing.com/ Bing international version website entrance: https://global.bing.com/ How to access Bing international version? 1. First enter the URL to open Bing: https://www.bing.com/ 2. You can see that there are options for domestic and international versions. We only need to select the international version and enter keywords.

What is the difference between php5 and php8 What is the difference between php5 and php8 Sep 25, 2023 pm 01:34 PM

The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

How to use Bing Image Creator for free How to use Bing Image Creator for free Feb 27, 2024 am 11:04 AM

This article will introduce seven ways to get high-quality output using the free BingImageCreator. BingImageCreator (now known as ImageCreator for Microsoft Designer) is one of the great online artificial intelligence art generators. It generates highly realistic visual effects based on user prompts. The more specific, clear, and creative your prompts are, the better the results will be. BingImageCreator has made significant progress in creating high-quality images. It now uses Dall-E3 training mode, showing a higher level of detail and realism. However, its ability to consistently produce HD results depends on several factors, including fast

How to change port 80 in php5 How to change port 80 in php5 Jul 24, 2023 pm 04:57 PM

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

How to Talk to Bing AI: Workaround Instructions How to Talk to Bing AI: Workaround Instructions Apr 16, 2023 pm 12:04 PM

2023 will be known as the year of the AI ​​chatbot. First, the successful public launch of ChatGPT, and now the integration of GPT-4 into the new Bing, has everyone wanting to jump on the bandwagon and access the latest AI chatbots directly from their browser. But how do you chat with Bing AI? Can you talk to it using your voice alone? Here's everything you need to know and more. Get the new Bing Currently, Microsoft is only rolling out the new Bing to a select few users. However, once measured

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

See all articles