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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:51:42+00:00 2026-05-26T08:51:42+00:00

I have a question about PHP and the use of pointers and variables. The

  • 0

I have a question about PHP and the use of pointers and variables.

The following code produces something I wouldn’t have expected:

<?php
$numbers = array('zero', 'one', 'two', 'three');

foreach($numbers as &$number)
{
  $number = strtoupper($number);
}

print_r($numbers);

$texts = array();
foreach($numbers as $number)
{
  $texts[] = $number;
}

print_r($texts);
?>

The output is the following

Array
(
    [0] => ZERO
    [1] => ONE
    [2] => TWO
    [3] => THREE
)
Array
(
    [0] => ZERO
    [1] => ONE
    [2] => TWO
    [3] => TWO
)

Notice the ‘TWO’ appearing twice in the second array.

It seems that there is a conflict between the two foreach loops, each declaring a $number variable (once by reference and the second by value).

But why ? And why does it affect only the last element in the second foreach ?

  • 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-26T08:51:42+00:00Added an answer on May 26, 2026 at 8:51 am

    The key point is that PHP does not have pointers. It has references, which is a similar but different concept, and there are some subtle differences.

    If you use var_dump() instead of print_r(), it’s easier to spot:

    $collection = array(
        'First',
        'Second',
        'Third',
    );
    
    foreach($collection as &$item){
        echo $item . PHP_EOL;
    }
    
    var_dump($collection);
    
    foreach($collection as $item){
        var_dump($collection);
        echo $item . PHP_EOL;
    }
    

    … prints:

    First
    Second
    Third
    array(3) {
      [0]=>
      string(5) "First"
      [1]=>
      string(6) "Second"
      [2]=>
      &string(5) "Third"
    }
    array(3) {
      [0]=>
      string(5) "First"
      [1]=>
      string(6) "Second"
      [2]=>
      &string(5) "First"
    }
    First
    array(3) {
      [0]=>
      string(5) "First"
      [1]=>
      string(6) "Second"
      [2]=>
      &string(6) "Second"
    }
    Second
    array(3) {
      [0]=>
      string(5) "First"
      [1]=>
      string(6) "Second"
      [2]=>
      &string(6) "Second"
    }
    Second
    

    Please note the & symbol that’s left in the last array item.

    To sum up, whenever you use references in a loop, it’s good practice to remove them at the end:

    <?php
    
    $collection = array(
        'First',
        'Second',
        'Third',
    );
    
    foreach($collection as &$item){
        echo $item . PHP_EOL;
    }
    unset($item);
    
    var_dump($collection);
    
    foreach($collection as $item){
        var_dump($collection);
        echo $item . PHP_EOL;
    }
    unset($item);
    

    … prints the expected result every time.

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

Sidebar

Related Questions

I have a question about PHP sessions. I use a session to keep a
A quick question about best practice with PHP classes. I have seen people use
I have a question about using os.execvp in Python. I have the following bit
I have a question about PHP's fopen() function (using php://output). Server A uses this
Further to my previous question about salted passwords in PHP/MySQL , I have another
My question is about an error I have when I use jQuery.ajax in my
I have a question about the use off the http api from the company
I have a quick question about mysql_real_escape_string. Where should I use it? I have
I have asked a few question lately about the use of the singleton and
I have a question about PHP require() function. I have a PHP script called

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.