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
首先创建变量$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
首先创建变量$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
在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;

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools