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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:52:26+00:00 2026-05-22T12:52:26+00:00

$GLOBALS[items] = array(‘one’, ‘two’, ‘three’, ‘four’, ‘five’ ,’six’, ‘seven’); $alter = &$GLOBALS[items]; // Comment

  • 0
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven');
$alter = &$GLOBALS["items"]; // Comment this line
foreach($GLOBALS["items"] as $item) {
  echo get_item_id();
}

function get_item_id(){
  var_dump(key($GLOBALS["items"]));
}

Check output of this code, with commented and uncommented second line.
My result(PHP 5.3.0).
With second line

int(1) int(2) int(3) int(4) int(5) int(6) NULL

Without second line:

int(1) int(1) int(1) int(1) int(1) int(1) int(1)

Why so strange result?

  • 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-22T12:52:26+00:00Added an answer on May 22, 2026 at 12:52 pm

    Here is a possible explanation:

    We know that foreach always loops over a copy of the array if it is not referenced:

    Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer.

    That means that the internal pointer of the original array is not changed and key() will always return the same value (as we can see when we comment out the line). And indeed if we do a var_dump($GLOBALS), we get:

     ["items"]=>
      array(7) {
        [0]=>
        string(3) "one"
        [1]=>
        string(3) "two"
        [2]=>
        string(5) "three"
        [3]=>
        string(4) "four"
        [4]=>
        string(4) "five"
        [5]=>
        string(3) "six"
        [6]=>
        string(5) "seven"
      }
    

    (no reference)

    But as soon as we generate a reference to the array (with $alter), $GLOBALS['items'] becomes a reference too, because both entries have to point to the same array:

     ["items"]=>
      &array(7) {
        [0]=>
        string(3) "one"
        [1]=>
        string(3) "two"
        [2]=>
        string(5) "three"
        [3]=>
        string(4) "four"
        [4]=>
        string(4) "five"
        [5]=>
        string(3) "six"
        [6]=>
        string(5) "seven"
      }
      ["alter"]=>
      &array(7) {
        [0]=>
        string(3) "one"
        [1]=>
        string(3) "two"
        [2]=>
        string(5) "three"
        [3]=>
        string(4) "four"
        [4]=>
        string(4) "five"
        [5]=>
        string(3) "six"
        [6]=>
        string(5) "seven"
      }
    

    Hence, the foreach loop does iterate over the original array and changes the internal pointer, which affects key().


    To sum up: It is a problem with references, not with $GLOBALS.

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

Sidebar

Related Questions

i have to function in two different files. one of them should add a
I am iterating over an array in MooTools but seeing additional items when iterating
I've tried making a global array that can add and remove items across all
I saw this question asking about whether globals are bad . As I thought
Is there a better way than using globals to get interesting values from a
I'm aware of using globals(), locals() and getattr to referance things in Python by
I have the following method in an App_Code/Globals.cs file: public static XmlDataSource getXmlSourceFromOrgid(int orgid)
Are static variables stored on the stack itself similar to globals? If so, how
I have PHP configured so that magic quotes are on and register globals are
A class project involves sorting an array of strings, with each string containing an

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.