Project

General

Profile

Bug #20995 » test-foreach-reference-bug.php

Administrator Admin, 2009-09-21 10:21

 
<?

/* checks PHP behaviour on reference values in foreach loops
* by Ernesto Baschny <ernst@cron-it.de>
*/

// Test function (from t3lib_div):
function addSlashesOnArray(array &$theArray) {
foreach ($theArray as &$value) {
if (is_array($value)) {
addSlashesOnArray($value);
} else {
$value = addslashes($value);
}
#unset($value);
}
reset($theArray);
}

// create some array with multiple values
$arr = array('a', 'b');
echo "orig:\n"; var_dump($arr);

// apply the
addSlashesOnArray($arr);

// make a copy of the array and modify first item (we expect it to apply to copy only!):
$copy = $arr;
$copy[0] = 'X';

// not check if original was changed by the modification in the copy:
echo "\ncopy:\n"; var_dump($copy);
echo "\norig (should be the same as the orig):\n"; var_dump($arr);

?>
(3-3/3)