PHP发布和预递增

编程入门 行业动态 更新时间:2024-10-26 06:34:06
本文介绍了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规范: github/php/php- langspec/blob/master/spec/10-expressions.md

The result should be unspecified. Please read the following PHP specification: github/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发布和预递增

本文发布于:2023-10-12 01:18:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!