PHP 中,运算符的问题 $a + $a++ 和 $a + $a + $a++ 都等于 3?

WBOY
Release: 2016-06-06 20:48:51
Original
1133 people have browsed it

$a = 1; 为何 $a + $a++$a + $a + $a++ 都等于 3? 图: PHP 中,运算符的问题 $a + $a++ 和 $a + $a + $a++ 都等于 3?

回复内容:

$a = 1; 为何 $a + $a++$a + $a + $a++ 都等于 3? 图: PHP 中,运算符的问题 $a + $a++ 和 $a + $a + $a++ 都等于 3?

又一个PHP操作符优先级的问题

具体请查阅:运算符优先级

第一种情况:

<code class="lang-php">$a = 1;
var_dump($a + $a++); 
</code>
Copy after login

此时$a++先运算;$a + $a++ 等价于:2 + 1 所以结果为3;

第二种情况:

<code class="lang-php">$a = 1;
var_dump($a + $a + $a++); 
</code>
Copy after login

此时从左至右运算,$a + $a 先运算,得出结果:2,并且将结果保存。 然后在运算$a++(此时前面已经计算好的$a+$a=2的值不受影响) ,以上等价于:2 + 1 结果依旧是3

最后,哪里来那么多这样无聊的坑爹的问题?

最最后 PHP 操作符优先级别真的很蛋疼!!!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!