【问题标题】:PHP post- increment/descrement operator precedence [duplicate]PHP后递增/递减运算符优先级[重复]
【发布时间】:2013-06-18 16:04:15
【问题描述】:

在文档http://www.php.net/manual/en/language.operators.precedence.php 中说,++-- 运算符具有非常高的优先级。 但据我所知,++$x$x++ 并不相同。此外,$x++ 应该有最小的优先级,因为它是在一切完成后计算的:

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

那么,后自增/自减运算符应该在这张表的底部?

【问题讨论】:

  • 这就是为什么我从来没有用任何语言写过这样的代码,不管它多么紧凑:)
  • @zoltan-tamasi 我要去参加 ZCE 考试,所以我需要知道这一点。在现实生活中,普通人永远不会这样做:)

标签: php operator-precedence


【解决方案1】:

是的。如果运算符放在变量之前,则变量在任何其他操作顺序之前更改。

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

当运算符在前面时,它优先于所有其他运算符。 您也可以在以下网站找到一个简单的解释:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-29
    • 2013-06-18
    • 2017-07-18
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2019-12-06
    相关资源
    最近更新 更多