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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:16:23+00:00 2026-05-27T00:16:23+00:00

I just had some very strange behavior with a simple php script I was

  • 0

I just had some very strange behavior with a simple php script I was writing. I reduced it to the minimum necessary to recreate the bug:

<?php

$arr = array("foo",
             "bar",
             "baz");

foreach ($arr as &$item) { /* do nothing by reference */ }
print_r($arr);

foreach ($arr as $item) { /* do nothing by value */ }
print_r($arr); // $arr has changed....why?

?>

This outputs:

Array
(
    [0] => foo
    [1] => bar
    [2] => baz
)
Array
(
    [0] => foo
    [1] => bar
    [2] => bar
)

Is this a bug or some really strange behavior that is supposed to happen?

  • 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-05-27T00:16:24+00:00Added an answer on May 27, 2026 at 12:16 am

    After the first foreach loop, $item is still a reference to some value which is also being used by $arr[2]. So each foreach call in the second loop, which does not call by reference, replaces that value, and thus $arr[2], with the new value.

    So loop 1, the value and $arr[2] become $arr[0], which is ‘foo’.
    Loop 2, the value and $arr[2] become $arr[1], which is ‘bar’.
    Loop 3, the value and $arr[2] become $arr[2], which is ‘bar’ (because of loop 2).

    The value ‘baz’ is actually lost at the first call of the second foreach loop.

    Debugging the Output

    For each iteration of the loop, we’ll echo the value of $item as well as recursively print the array $arr.

    When the first loop is run through, we see this output:

    foo
    Array ( [0] => foo [1] => bar [2] => baz )
    
    bar
    Array ( [0] => foo [1] => bar [2] => baz )
    
    baz
    Array ( [0] => foo [1] => bar [2] => baz )
    

    At the end of the loop, $item is still pointing to the same place as $arr[2].

    When the second loop is run through, we see this output:

    foo
    Array ( [0] => foo [1] => bar [2] => foo )
    
    bar
    Array ( [0] => foo [1] => bar [2] => bar )
    
    bar
    Array ( [0] => foo [1] => bar [2] => bar )
    

    You’ll notice how each time array put a new value into $item, it also updated $arr[3] with that same value, since they are both still pointing to the same location. When the loop gets to the third value of the array, it will contain the value bar because it was just set by the previous iteration of that loop.

    Is it a bug?

    No. This is the behavior of a referenced item, and not a bug. It would be similar to running something like:

    for ($i = 0; $i < count($arr); $i++) { $item = $arr[$i]; }
    

    A foreach loop isn’t special in nature in which it can ignore referenced items. It’s simply setting that variable to the new value each time like you would outside of a loop.

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

Sidebar

Related Questions

I'm working with shaders just now and I'm observing some very strange behavior. I've
I just had some basic php questions to further my understanding as I learn,
I've just parallelized a fortran routine that simulates individuals behavior and I've had some
I had some very strange problems with GraphicsGrid. The individual PraphPlot:s looks nice and
I was just reading this article and it mentions that some organization had an
I had some very wrong sounding advice recently from a senior developer/coworker regarding the
I am just getting into Grails having had some experience with Rails, and I
Just getting started with jQuery, and I've had some success so far. I've created
I have recently started looking into MQs and I had some very basic questions,
I am recently experiencing some very strange behavious with Fash Builder 4.5. Sometime it

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.