Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nonequivalent code snippets #1371

Open
nikolatechie opened this issue Aug 4, 2024 · 2 comments
Open

Nonequivalent code snippets #1371

nikolatechie opened this issue Aug 4, 2024 · 2 comments

Comments

@nikolatechie
Copy link

Where is the problem?

  • In the last two code snippets.

What is the problem?

  • After $x = 0;, $y = $x++; is rewritten as $y = $x + 1;, $x++;. These snippets are not equivalent. In the first snippet, $y equals 0 after which $x gets incremented, whereas in the second snippet $y becomes 1 and $x gets incremented.

Please don't change anything below this point.


  • Build ID: HHVM=HHVM-4.164.0:HSL=v4.108.1:2024-02-08T13:44:46+0000:1fa47f258c6b68f8ec01899aa82fd6ffa0957109
  • Page requested: /hack/expressions-and-operators/incrementing-and-decrementing
  • Page requested at: Sun, 04 Aug 2024 19:54:28 +0000
  • Controller: GuidePageController
@lexidor
Copy link
Contributor

lexidor commented Aug 5, 2024

You are correct, $x++ is a post increment. The old value of $x gets used for the assignment. How would you like the statement to be rewritten? Should ++$x be used to restore the described behavior or should a different example be used altogether?

@nikolatechie
Copy link
Author

I would suggest just removing + 1 from the second code snippet, as this example would be the easiest to understand. That would look like this:

$x = 0;
$y = $x++; // Parse error

Instead, the above code must be written as statements.

$x = 0;
$y = $x;
$x++;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants