通过xdebug来分析PHP引用
一直没搞懂php的引用,今天面试的时候又碰到了,借助xdebug貌似理解了一点,记录一下。
?
code1:
?
$a = "xiaoshenge";$b = &$a;unset($b);echo "b=$b";echo "a=$a";
结果:b=a=xiaoshenge
?
code2:
$a = "xiaoshenge";$b = &$a;unset($a);echo "b=$b";echo "a=$a";
结果:b=xiaoshengea=??
?
猜测:
?
面试的时候是code1,由于没搞懂PHP的引用当时就猜测的写了b=a=,其实这与我把引用跟c里面的指针搞混了有关。回来之后,调试了一下code2,然后彻底迷惑了,于是求救与PHP文档。
?
PHP文档中对于引用的介绍:
?
?
引用是什么:在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字。最接近的比喻是 Unix 的文件名和文件本身——变量名是目录条目,而变量内容则是文件本身。引用可以被看作是 Unix 文件系统中的 hardlink。
?
取消引用:当 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:
?
<?php $a = 1;$b =& $a;unset($a);?>
?不会 unset?$b,只是?$a。再拿这个和 Unix 的?unlink?调用来类比一下可能有助于理解。
?
推断:
?
文档里面有介绍”引用可以被当作是unix文件系统中的hardlink“,参考http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=150986?中的介绍:
硬连接是给文件一个副本,同时建立两者之间的连接关系。修改其中一个,与其连接的文件同时被修改。如果删除其中[color=red]任意一个[/color]其余的文件将不受影响。?
软连接也叫符号连接,他只是对源文件在新的位置建立一个“快捷(借用一下wondows常用词)”,所以,当源文件删除时,符号连接的文件将成为无源之水->仅仅剩下个文件名了,当然删除这个连接,也不会影响到源文件,但对连接文件的使用、引用都是直接调用源文件的。?
?
通过xdebug来看zval容器中的变化:
?
code1:
?
$a = "xiaoshenge";$b = &$a;xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );unset($b);xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );
结果:
?
a:
<em>(refcount=2, is_ref=1)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
b:
<em>(refcount=2, is_ref=1)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
a:
<em>(refcount=1, is_ref=0)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
?
code2:
?
$a = "xiaoshenge";$b = &$a;xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );unset($a);xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );
?结果:
a:
<em>(refcount=2, is_ref=1)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
b:
<em>(refcount=2, is_ref=1)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
b:
<em>(refcount=1, is_ref=0)</em>,<small>string</small> <span style="color: #cc0000;">'xiaoshenge'</span> <em>(length=10)</em>
?
?
结合unix的硬链接来看(应该如下):
unset($a),只是销毁了a-x部分所以$b还在。
?
$b=&$b,不是指针那样,$b指向$a,(指针,貌似我的C都还给书了,要补习了)
?
面试感受:不能人云亦云,要自己动手,自己思考。PHP手册不能轻视,这个问题文档里面有。

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.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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