刚入门php,请教一个运算符中$a++,++$a,$a--,--$a,的问题

WBOY
Release: 2016-06-06 20:44:10
Original
1426 people have browsed it

<code>$a=21;
$b=$a++;    
$c=++$a;     
echo $a;    
echo $b;    
echo $c;    
</code>
Copy after login
Copy after login

我理解的结果过是23 ,22,23,

但是输入的结果为什么是23,21,23 求解

回复内容:

<code>$a=21;
$b=$a++;    
$c=++$a;     
echo $a;    
echo $b;    
echo $c;    
</code>
Copy after login
Copy after login

我理解的结果过是23 ,22,23,

但是输入的结果为什么是23,21,23 求解

这不是PHP的问题,程序语言都这样。$a++是先做运算再自增的,++$a是先自增再做运算

++前置就先自增 后置就后自增
具体拆开等价于下面这个:

<code class="lang-php">$a = 21;

//$b = $a++;
$b = $a;    
$a++;

//$c = ++$a;
++$a;
$c = $a;  
</code>
Copy after login
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