与数组索引中的$ b ++一起使用时,PHP为什么对$ b和$ b = $ b求值的方式不同 [英] Why does PHP evaluate $b and $b = $b differently when used with $b++ in array index

查看:110
本文介绍了与数组索引中的$ b ++一起使用时,PHP为什么对$ b和$ b = $ b求值的方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法掌握下面列出的代码中的评估逻辑.有谁知道为什么PHP在这种情况下对$b$b = $b的评价不同?

I am unable to grasp the evaluation logic in the code listed below. Does anyone know why PHP evaluates $b and $b = $b differently in this case?

我在SO处阅读了许多问题,并查看了 PHP手册.这样做,我了解到"PHP不会(通常情况下)指定对表达式进行评估的顺序" ,并且行为可能会在PHP版本或取决于周围的代码" .我不认为这适用于这种情况.还是呢?

I have read through a number of questions here at SO and checked the PHP manual. Doing so I've come to understand that "PHP does not (in the general case) specify in which order an expression is evaluated" and that "the behavior can change between versions of PHP or depending on the surrounding code". I don't feel that that applies to this situation though. Or does it?

第一个承认这可能不是您每天的编码问题,但我仍然很好奇.偶然发现它尝试做一些打高尔夫球的代码.

Being the first to admit this may not be your everyday coding issue, I am still curious. Stumbled upon it trying to do some code golfing.

$a = [[00, 01, 02, 03],
      [10, 11, 12, 13],
      [20, 21, 22, 23],
      [30, 31, 32, 33]];

$b = 2;
echo $a[$b][$b++], PHP_EOL;

$b = 2;
echo $a[$b=$b][$b++], PHP_EOL;

输出-PHP 5.5.14:

Output - PHP 5.5.14:

32
22

推荐答案

这看起来像手册用于演示未定义的评估顺序.从手册中:

This looks like the example in the manual used for demonstrating Undefined order of evaluation. From the manual:

运算符的优先级和关联性仅决定表达式的分组方式,它们不指定评估顺序. PHP(通常)不指定表达式的评估顺序和编码方式假设应避免使用特定的评估顺序,因为行为可能在PHP版本之间或取决于周围的代码而改变.

Operator precedence and associativity only determine how expressions are grouped, they do not specify an order of evaluation. PHP does not (in the general case) specify in which order an expression is evaluated and code that assumes a specific order of evaluation should be avoided, because the behavior can change between versions of PHP or depending on the surrounding code.

添加了重点

他们给的例子:

<?php
$a = 1;
echo $a + $a++; // may print either 2 or 3

$i = 1;
$array[$i] = $i++; // may set either index 1 or 2
?>

之所以得到输出,是因为在第一个示例中,索引$b++是首先确定的 ,而在第二个示例中,索引$b=$b是首先确定的.

You are getting the output you are because in the first example, the index $b++ is being determined first, whereas in the second one the index $b=$b is first.

NB

关于为什么这是,我相信在同一手册页上的此注释可以解释一个可能的原因:

As for why this is, I believe one possible reason is explained by this note on the same manual page:

尽管=的优先级低于大多数其他运算符,但PHP仍将允许类似于以下内容的表达式:if(!$ a = foo()),在这种情况下,foo()的返回值将放入$ a中.

Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.

我相信他们在那儿遗漏了一个至关重要的最后一个词: first (对我而言,阅读笔记失去了一点意义).

I believe they're missing a crucial last word there: first (without, to me reading the note loses a bit of meaning).

按照PHP自己的规则,,我们假设FIFO顺序为!$a,首先应对其进行评估.如果$a当前是nullundefined,则!$a将等于true(将对其求值,并且该结果将被丢弃).然后,将评估foo(),并将其返回值分配给$a(即使我们假设是FIFO,如果要将其结果分配给某些内容,也必须首先评估foo()).赋值的结果将由if评估,并将得出与作者想要的值完全相反的值.

Following PHP's own rules, and we assume a FIFO order, !$a should be evaluated first. If $a is currently null or undefined, then !$a will equal true (it will be evaluated and that result will be thrown away). Following that, foo() will be evaluated, and its return value will be assigned to $a (even if we're assuming FIFO, foo() must be evaluated first if its result is to be assigned to something). The result of this assignment will be evaluated by if, and will result in exactly the opposite value that the author wanted.

我不是C专家,但是一些搜索也使我得到这个答案,其中引用了C90标准:

I'm no C expert, but a bit of searching also lead me this answer which quotes the C90 Standard:

(C90,6.3)除非语法指示或稍后另行指定(对于函数调用运算符(),&&,||,?:和逗号运算符).子表达式和副作用发生的顺序都是未指定的"

(C90, 6.3) "Except as indicated by the syntax or otherwise specified later (for the function-call operator (), &&, ||, ?:, and comma operators). the order of evaluation of subexpressions and the order in which side effects take place are both unspecitied"

由于PHP是基于C构建的,因此可以继承其某些怪癖是很有意义的.

Since PHP is built on C, it makes sense that it would inherit some of its eccentricities.

这篇关于与数组索引中的$ b ++一起使用时,PHP为什么对$ b和$ b = $ b求值的方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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