|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-03-28 14:35 UTC] hitchiker at mail dot ru
Description:
------------
I was sure that arrays are passed to functions by value, not by reference. It looks like after by-reference iteration changes array not only isnide foreach statement, but array remains changed even after iteration.
Test script:
---------------
<?php
$a = array(1,2,3,4);
function myf($a,$k){
$a[$k] = 9;
}
foreach ($a as $k => &$v) {
myf($a, $k);
}
//myf($a, 3);
var_dump($a);
die();
Expected result:
----------------
array
0 => int 1
1 => int 2
2 => int 3
3 => int 4
Actual result:
--------------
array
0 => int 9
1 => int 9
2 => int 9
3 => &int 9
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Sat Feb 07 18:00:01 2026 UTC |
I have similar problem: $tags = array('iphone', 'apple', 'iPhone 4'); foreach ($tags as &$v) $v = trim($v); $tags2 = $tags; foreach ($tags2 as &$a) $a = '*'; print_r($tags); Result: Array ( [0] => iphone [1] => apple [2] => * )