php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68240 php class vars have a bug in foreach
Submitted: 2014-10-16 09:11 UTC Modified: 2014-10-16 09:41 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: 498936940 at qq dot com Assigned:
Status: Not a bug Package: PHP Language Specification
PHP Version: 5.6Git-2014-10-16 (snap) OS: WIN 8.1
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: 498936940 at qq dot com
New email:
PHP Version: OS:

 

 [2014-10-16 09:11 UTC] 498936940 at qq dot com
Description:
------------
class a{
	public $s = array('a' => 'a%se');
	
	function aa(){
		foreach(range(1,5) as $v){
			$this->s['a'] = str_replace('%s', $v, $this->s['a']);
			var_dump($this->s);
		}
	}
}

$a = new a();
$a->aa();


result always:

array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}

i think right:
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a2e"
}
array(1) {
  ["a"]=>
  string(3) "a3e"
}
array(1) {
  ["a"]=>
  string(3) "a4e"
}
array(1) {
  ["a"]=>
  string(3) "a5e"
}



Test script:
---------------
class a{
	public $s = array('a' => 'a%se');
	
	function aa(){
		foreach(range(1,5) as $v){
			$this->s['a'] = str_replace('%s', $v, $this->s['a']);
			var_dump($this->s);
		}
	}
}

$a = new a();
$a->aa();


result always:

array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}

i think right:
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a2e"
}
array(1) {
  ["a"]=>
  string(3) "a3e"
}
array(1) {
  ["a"]=>
  string(3) "a4e"
}
array(1) {
  ["a"]=>
  string(3) "a5e"
}



Expected result:
----------------
class a{
	public $s = array('a' => 'a%se');
	
	function aa(){
		foreach(range(1,5) as $v){
			$this->s['a'] = str_replace('%s', $v, $this->s['a']);
			var_dump($this->s);
		}
	}
}

$a = new a();
$a->aa();


result always:

array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}

i think right:
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a2e"
}
array(1) {
  ["a"]=>
  string(3) "a3e"
}
array(1) {
  ["a"]=>
  string(3) "a4e"
}
array(1) {
  ["a"]=>
  string(3) "a5e"
}



Actual result:
--------------
class a{
	public $s = array('a' => 'a%se');
	
	function aa(){
		foreach(range(1,5) as $v){
			$this->s['a'] = str_replace('%s', $v, $this->s['a']);
			var_dump($this->s);
		}
	}
}

$a = new a();
$a->aa();


result always:

array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a1e"
}

i think right:
array(1) {
  ["a"]=>
  string(3) "a1e"
}
array(1) {
  ["a"]=>
  string(3) "a2e"
}
array(1) {
  ["a"]=>
  string(3) "a3e"
}
array(1) {
  ["a"]=>
  string(3) "a4e"
}
array(1) {
  ["a"]=>
  string(3) "a5e"
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-16 09:41 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2014-10-16 09:41 UTC] bwoebi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You are overwriting $this->s['a'] in the first iteration of foreach to a1e. And as 
in a1e there's no %s, it remains unchanged in further iterations.
 [2014-10-16 09:45 UTC] guoxuivy at gmail dot com
unable to find ā€œ%sā€ after first update
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Sep 20 02:03:37 2021 UTC