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 8324739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:10:56+00:00 2026-06-09T00:10:56+00:00

This is propably a farcical problem but I just don’t see the reason for

  • 0

This is propably a farcical “problem” but I just don’t see the reason for this behaviour.


Facts:

$i++;

returns the current value and then increments $i by one.

++$i;

increments $i by one and then returns $i.


Situations:

for($i = 0; $i < 10; ++$i){
    echo $i."\n";
}

gives

0
1
2
3
4
5
6
7
8
9

2nd:

for($i = 0; $i < 10; $i++){
    echo $i."\n";   
}

gives also

0
1
2
3
4
5
6
7
8
9

If I’d take the documentation of the increment literally, I would explain the loops as follows:

  1. at the end of each iteration, $i is incremented by one and then
    returned, so we’ve got at first a 0 because $i started as 0, then a 1 etc.
  2. at the end of each iteration, $i is returned and THEN incremented,
    which would, exactly, mean that there were two iterations where $i =
    0.

It’s a fact that this is not true. Could somebody please explain, why?

  • 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-09T00:10:57+00:00Added an answer on June 9, 2026 at 12:10 am

    $i is not being returned, it is being used (by you). Big difference.

    If you were to rewrite your if statements to instead use a while loop, you’d have these:

    Post-increment:

    $i = 0;
    
    while ( $i < 10 ) {
        echo $i."\n";
        $i++;
    }
    

    Pre-increment:

    $i = 0;
    
    while ( $i < 10 ) {
        echo $i."\n";
        ++$i;
    }
    

    As you can see, there’s no difference between the two.


    Here’s what the 3 statements you supply to the for loop are for:

    1. Run before the first step
    2. Run before every step (use the returned value to determine whether to continue with the loop)
    3. Run after every step

    at no point is the returned value of the 3rd statement being utilized in any way.


    P.S. As mentioned, the third statement is run after every loop including the last one. This is evident by accessing the $i variable after the loop has completed:

    for ($i = 0; $i < 10; $i++) {
        echo $i."\n";
    }
    echo $i;
    

    which would list all the numbers up to and including 10.

    See it here in action: http://viper-7.com/Y6N2jU

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

Sidebar

Related Questions

This is propably just idea issue. I have many textViews in one ScrollLayout (so
This is propably a simple task to solve in Symfony2 but I am really
Okay. This is propably very basic and noobie, but I have about 20+ stores
This probably looks like a duplicate but I don't think so. I have already
This probably sounds like a terrible idea at first glance, but here is my
This probably is a dummy question but I cannot find a clear indication. I
I know this probably really simple but Im not sure what im doing wrong...
I know this probably has been asked before but I am having issues with
Im sorry for this probably dumm question, but I want to simply open modals
I am using ASP.Net MVC but this probably applies to all MVC patterns in

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.