php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68145 For + Array declaration inside
Submitted: 2014-10-03 14:53 UTC Modified: 2014-10-03 17:18 UTC
From: renzo dot disalvo at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.5.17 OS: Ubuntu 12.04 LTS (GNU/Linux 3.2
Private report: No CVE-ID: None
 [2014-10-03 14:53 UTC] renzo dot disalvo at gmail dot com
Description:
------------
I'm trying to generate some values and after assign to the array,

The strange think is when I print out the output is totally different.

In the final array I can see duplicate, but the original assignment is unique.

Test script:
---------------
        
        for ($i = 1; $i <= 10; $i++) {
            $question = $this->getRandomQuestion();
            print_r($question->data['name']); // return Correct Name
            $arrQuestion[$i] = $question;
            print_r($arrQuestion[$i]->data['name']); //Return Correct name
            print_r("\n");
        }
print_r($arrQuestion[1]); //Return Different Value


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-03 15:52 UTC] renzo dot disalvo at gmail dot com
Ok resolved but I don't know if is a bug,

Never mix object and array mixed together.

The strange think the output was correct but outside of the for the array become corrupted.

R
 [2014-10-03 17:18 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-10-03 17:18 UTC] requinix@php.net
There are many possible explanations for what happened and what you were seeing, and it may come down to what getRandomQuestion() is returning and how it does that. My guess: since objects in PHP 5 are references and not copies, if you modify the same object over and over again then every instance you had of that object will be affected too.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 14:00:01 2026 UTC