Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7746849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:23:07+00:00 2026-06-01T10:23:07+00:00

The php manual claims that: $a = 1; echo ++$a + $a++; is ambiguous

  • 0

The php manual claims that:

$a = 1;
echo ++$a + $a++;

is ambiguous under its grammar, but it seems extremely clear to me. ++$a and $a++ evaluate first, from left to right, so ++$a increments and then returns 2, and $a++ returns 2 and then increments. The sum of 2 + 2 is 4, so it would echo 4. However, The PHP Manual says very clearly that it may print 4 or 5.

Does the php spec not specify that operations will be performed from left to right?

Even if it doesn’t enforce that operations will be performed from left to right, in this case, wouldn’t it return 4 regardless?

Edit: I reread the page, and it stated that it is determined by each specific operator. + has lowest precedence, and evaluates from left to right, so it seems like my earlier assumption was correct. I still do not understand.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:23:08+00:00Added an answer on June 1, 2026 at 10:23 am

    Please note that this is just my humble opinion.

    I think the idea beneath this result is that none of the aperands has precedence when there’s a single operator and that in an operation a variable is kept as a reference instead of being replaced by its result during all the calculation until the last one (plus, in this example). So when it goes from l-r:

    $a = 1;
    ++$a + $a++
    operand 1 --> ++$a ==> $a = ++1 = 2
    result (where $a = 2) --> 2 + (2++) = 4
    

    whereas otherwise:

    $a = 1;
    ++$a + $a++
    operand 2 --> $a++ ==> $a = 1
    // new operation on the left side
    // so the value gets incremented ==> $a = 2
    result (where $a = 2) --> (++2) + 2 = 5
    

    I’m not sure about this, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP manual doesn't say that there is one, but... maybe there's a hidden one?
I have read the PHP manual , but couldn't find any proper explanation regarding
Maybe I'm missing it somewhere in the PHP manual, but what exactly is the
It seems like the following from the PHP manual regarding for loops is incorrect.
As I understood from PHP manual page for DatePeriod class that it is purposed
In the PHP manual for include , there is a user contribution that states
That's about all I need to ask. Checked the PHP manual and saw a
I have seen the PHP manual. But I don't understand the difference in behaviour
In PHP manual given example is echo substr_compare(abcde, bcg, 1, 2); // 0 I
I found a reference in the PHP manual, but my interpretation is pretty much

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.