简体   繁体   English

什么是PHP中的$ x +++ $ X ++? 不寻常地使用增量

[英]What is $x+++$X++ in PHP? Unusual use of increment

Few days ago i was on interview and I had one task to do with, to me, unknown use of increment. 几天前,我正在接受采访,我有一项任务,对我而言,未知使用增量。 It goes exactly like this: 它完全是这样的:

echo $x+++$x++;

I've never seen something like this before, I'm trying to figure out what it does and I'm clueless. 我以前从未见过这样的东西,我正在试图弄清楚它做了什么,而且我一无所知。 To check it i wrote such code sample: 为了检查它我写了这样的代码示例:

<?php
$x = 7;

echo $x. "<br>";
echo $x+++$x++;
?>

First line prints out what it is supposed to print out: 7 . 第一行打印出应该打印的内容: 7
But second line prints out 15 . 但第二行打印出15 Why? 为什么? How does this thing work? 这件事怎么样? What does it mean? 这是什么意思?

Is it just me or is it similar thing as variable variable which is funny task from interviewers but is not useful in reality? 它只是我还是类似于变量变量的东西,这是来自采访者的有趣任务,但在现实中没用?

It might be easier if you break it up a bit. 如果你稍微分解它可能会更容易。

PHP recognizes $x++ as an increment. PHP将$x++识别为增量。 An additional + after that is recognized as an addition. 之后的额外+被识别为添加。 Having

$x+++$x++

would be equal to 等于

$x++ + $x++

This is a post-increment, which means that you return the value - and then increment the value by one. 这是一个后增量,这意味着您返回值 - 然后将值增加1。

Step by step it'd be executed like this 它会一步一步地执行

  • Set $x = 7 设置$x = 7
  • $++ takes place, which returns the value it had before incrementing, 7, then increments $x by one $++ ,返回增值前的值,7, 然后将 $x递增1
  • $x = 7 was returned, but , $x is now 8 (due to post-increment) 返回$x = 7但是$x现在是8(由于后增量)
  • Addition of $x with the value it had before the increment, added with $x after the increment (because 7 was returned from the first $x++ , but $x was naturally incremented, after the first increment $x++ , $x becomes 8 - and that's now the value of the second $x before that final post-increment takes place) 加成$x与它的增量之前的值,加入$x增量后(因为7是从第一个返回$x++ ,但$x是自然递增,第一个增量后$x++$x变为8 -现在是最后一次增量之前的第二个$x的值)
  • You get the addition of 7 + 8 = 15, and print that 你得到7 + 8 = 15的加法,并打印出来
  • Increment $x one last time, making it $x = 9 (which is somewhat irrelevant as you don't use $x after that) 最后一次增加$x ,使得$x = 9 (这有点无关紧要,因为你之后不使用$x

Have a look at the manual for operator precedence and increment/decrementing operators. 查看运算符优先级和递增/递减运算符的手册。

It's just formatted funny. 它的格式很有趣。 You can format it as such: 您可以将其格式化为:

echo $x++ + $x++;

$x++ returns the value and then increments, so it's 7. Then, you add it to $x++ again. $x++返回值然后递增,因此它是7.然后,再次将它添加到$x++ By now, $x is 8. So you get 7 + 8 = 15. If you echo $x; 到目前为止, $x是8.所以你得到7 + 8 = 15.如果你echo $x; again, you'll get 9 (it incremented after returning the value in the last statement). 再次,你将得到9(它在返回最后一个语句中的值后递增)。

Read more about this in this great answer 在这个伟大的答案中阅读更多相关信息

It's X++ + X++ 这是X ++ + X ++

X++ means use X and then increment so 7+8 (second time using X the value has increased by 1) X ++表示使用X然后增加7 + 8(第二次使用X值增加1)

I'm not sure what they were trying to prove with such a test but they are not useful things to test (it's just knowing trivia) 我不确定他们试图通过这样的测试证明什么,但它们不是有用的东西来测试(它只是知道琐事)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM