关于PHP中的自增、自减操作的奥秘

WBOY
Release: 2016-06-23 13:48:05
Original
780 people have browsed it

首先看一道面试题:

    $a = 1;    $b = &$a;    if ($b == $a++)        echo "true";    else        echo "false";
Copy after login

首先,创建了一个变量$a,并且将数值设定为了1;

然后,创建了一个变量$b,并将其作为对$a的一个引用;

最后这个判断语句中,包含两个opcode:POST_INC 和 IS_EQUAL。首先执行的是先返回后自增语句(POST_INC),首先返回1,然后$a自增为了2,因为$b是$a的一个引用,$b也是2。然后执行的是比较语句(IS_EQUAL),因为$b的值是2,$a++的返回值是1,所以不相等。

相类似的面试题目还有:

    $a = 1;    $b = &$a;    $b = $a++;        echo "a: $a; b: $b";
Copy after login



Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template