Table of Contents
回复内容:
Home Backend Development PHP Tutorial 面试题 - PHP引用传递的问题,为什么是这样

面试题 - PHP引用传递的问题,为什么是这样

Jun 06, 2016 pm 08:30 PM
php Interview questions

1)为什么 $a 结果是hello 而不是 undefined?

<code>$a = "hello";
$b =& $a;
unset($b);
$b = "world";
var_dump($a); // hello
</code>
Copy after login
Copy after login

2)为什么 $a 结果是 1 而不是 2?

<code>$a = 1;
$x =& $a;
$b = $a++;
var_dump($b); // 1

</code>
Copy after login
Copy after login

回复内容:

1)为什么 $a 结果是hello 而不是 undefined?

<code>$a = "hello";
$b =& $a;
unset($b);
$b = "world";
var_dump($a); // hello
</code>
Copy after login
Copy after login

2)为什么 $a 结果是 1 而不是 2?

<code>$a = 1;
$x =& $a;
$b = $a++;
var_dump($b); // 1

</code>
Copy after login
Copy after login

1.参考php文档对unset的定义

如果在函数中 unset() 一个通过引用传递的变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。 php unset

所以unset $b对$a无影响

2.参考php文档对递增递减运算符的定义

$a++ 后加 返回 $a,然后将 $a 的值加一。 php递增/递减运算符

$a++先返回1赋值给$b,所以$b为1

1,b是a的快捷方式,马甲。快捷方式删除了不受影响。
2,$a++是表示语句执完才+1。你想得到2的结果改成$b = ++$a;或var_dump($a);即可

  • $b是对$a的引用,它们是两个不同的变量指向同一块物理地址。当执行unset($b)时,只是注销$b这个变量的指引而非注销物理地址中的值,所以这个时候$a依旧是原值hello.

  • 这个问题你要理解$a++++$a的区别。

<code>//$a++相当于
$b = $a;
$a += 1;

//++$a相当于
$a += 1;
$b = $a;
</code>
Copy after login

因此,$a++是先赋值后自增,所以$b的值为1

unset()函数只是unset掉$b对于$a的引用。
因此$a自己指向的空间依然会保留下来,只是对于$b的引用计数和is_ref标记被删除了。
因此$a并不符合垃圾回收机制回收的对象。
关于PHP的引用机制可以从引用计数基本知识中获取答案。

第一个是因为, unset() 一个通过引用传递的变量,只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。

第二个是运算优先级的问题, ++在前先自增后计算表达式, ++在后先计算表达式后自增

首先说一下PHP中变量的理解:变量可以看做一个盒子,而变量名相当于这个盒子的标签,应用传递只是相当于给同一个盒子贴了两个标签,值传递是重新制作了一个一模一样的盒子!
局部变量名是存放在栈内存中的,unset()函数只是将一个变量的指向地址和变量实际存放位置断开,即把盒子标签撕掉!所以unset($b)只是将$b和实际存放位置的联系给断开了,那么$b在栈内存中的空间就会被回收掉,但是这个盒子上还有$a这个标签,所以是不会被垃圾回收机制给回收的!所以你输出$a肯定依然还是能被找到(寻址正确)的,但是现在你输出$b的话就应该是找不到的了!
至于第二个为什么不是2,其他人的回答已经很清楚了!这个是前置运算符++$a和后置运算符$a++的区别!
++$a:先自增再执行对$a的一切操作
$a++:操作了$a后再自增

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles