PHP发布和预递增 [英] PHP Post and preincrement

查看:115
本文介绍了PHP发布和预递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中发现了一些奇怪的计算,例如:

I have found in PHP some strange calculation, for example this:

$c=5;

$r = $c + ($c++ + ++$c);

echo $r;

为什么结果是19而不是17?

Why result is 19 and not 17?

谢谢

推荐答案

结果应不确定.请阅读以下PHP规范:
https://github.com/php/php- langspec/blob/master/spec/10-expressions.md

The result should be unspecified. Please read the following PHP specification:
https://github.com/php/php-langspec/blob/master/spec/10-expressions.md

虽然优先级,关联性和分组括号控制着 应用运算符的顺序,他们不控制运算符的顺序 术语本身的评估.除非在此明确说明 规范,表达式中操作数的顺序 相对于彼此评估的是未指定的.见讨论 上面关于包含序列点的运算符. (例如, 在完整表达式$ list1 [$ i] = $ list2 [$ i ++]中,是否 左侧的$ i是旧的或新的$ i,未指定. 同样,在完整表达式$ j = $ i + $ i ++中,是否 $ i是旧的或新的$ i,未指定.最后,在完整 表达式f()+ g()* h(),这三个函数的顺序 ,则未指定).

While precedence, associativity, and grouping parentheses control the order in which operators are applied, they do not control the order of evaluation of the terms themselves. Unless stated explicitly in this specification, the order in which the operands in an expression are evaluated relative to each other is unspecified. See the discussion above about the operators that contain sequence points. (For example, in the full expression $list1[$i] = $list2[$i++], whether the value of $i on the left-hand side is the old or new $i, is unspecified. Similarly, in the full expression $j = $i + $i++, whether the value of $i is the old or new $i, is unspecified. Finally, in the full expression f() + g() * h(), the order in which the three functions are called, is unspecified).

您也可以在PHP文档中找到相同的理由:

You could find the same reasoning in PHP documentation too:

这篇关于PHP发布和预递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆