duanraotun1674 2013-06-21 05:37 采纳率: 100%
浏览 55
已采纳

PHP后递增/递减运算符优先级

In documentation http://www.php.net/manual/en/language.operators.precedence.php it is said, that ++ and -- operators have very high precedence. But as i know, ++$x and $x++ is not the same. Moreover, $x++ should have minimal precedence, because it is calculated after everything is done:

$x = 1;
var_dump(1 and $x--); // and operator is one of last operators in the table, it will be executed before post decrement

So, post- increment/decrement operators should be in this table in the bottom?

  • 写回答

1条回答 默认 最新

  • douzhangcuo2174 2013-06-21 05:44
    关注

    Yes. If the operators are placed before the variable then the variable is changed before any other order of operations.

    $a=4;
    $x=++$a + 6; will result in $x=11 and $a=5
    $x=$a++ + 6; will result in $x=10 and $a=5
    

    When the operators are in front it takes precedence over all other operators. You can find a simple explanation at the following site as well:

    http://www.php.net/manual/en/language.operators.increment.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?