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

  • SEARCH
  • Home
  • 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 8159857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:03:45+00:00 2026-06-06T18:03:45+00:00

<?php $options = array(); $currentYear = 2012; while($currentYear < (2012 + 3) ) {

  • 0
<?php
$options = array();
$currentYear = 2012;

while($currentYear < (2012 + 3) ) {
    $options[$currentYear++] = $currentYear;
}

var_dump($options);
?>

expected output:

array(3) {
     [2012]=> int(2012)
     [2013]=> int(2013)
     [2014]=> int(2014)
}

Generic theory: Executes the RHS of an assignment first and assigns the RHS value to LHS. It would execute post-increment in LHS after it executed the RHS. According to this scenario, we can explain the iteration as follows.

In 1st iteration, RHS $currentYear value is 2012 and assigns that value to array options with key 2012. Increment the variable $currentYear by 1, and proceed with the iteration. In 2nd iteration, RHS $currentYear value is 2013 and assigns that value to array options with key 2013. Increment the variable $currentYear by 1, and proceed with the iteration. What happened to this generic programming concept down below?

Actual output:

 array(3) {
     [2012]=> int(2013)
     [2013]=> int(2014)
     [2014]=> int(2015)
 }

If someone can come up with a better explanation that would be great, and much appreciate.

  • 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-06T18:03:46+00:00Added an answer on June 6, 2026 at 6:03 pm

    Above theory is wrong.
    In PHP arrays have higher precedence than increment/decrement. Therefore PHP executes the array keys first (that’s why first element of the array key appeared as 2012) and then goes to the assignment.

    In this scenario, PHP keeps array key as $currentYear value 2012 and increment by 1. Then it comes to the RHS takes $currentYear value and assigns it to the array options with the key 2012. Likewise, it continues with the iteration.

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

Sidebar

Related Questions

How do I extend my parent's options array for child classes in PHP? I
In cake php how to avoid one or more elements in option array of
I just signed up for BlueHost and one of their PHP config options is
I need to override an abstract Block in Magento : app/code/core/Mage/Eav/Block/Adminhtml/Attribute/Edit/Options/Abstract.php I need to
I have a html/php form like the following: Form 1: Apple options: -big -medium
I've reviewed a couple options for jquery-based RTE that required PHP. I'm running a
With PHP I echo out a <select> with options to a form based on
I have not changed any of the configuration options for sessions in my php.ini.
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have an HTTPservice id=myhttp url=site.com/script.php method=POST resultFormat=xml The script it uses returns $output

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.