note 116562 deleted from functions.returning-values by salathe
| From: | salathe@php.net | Date: | Fri, 06 Feb 2015 13:02:54 +0000 |
| Subject: | note 116562 deleted from functions.returning-values by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-201611@lists.php.net to get a copy of this message | ||
Note Submitter: meeme9 at gmail dot com
----
ref: Developers with a C background may expect pass by reference semantics for arrays. It may be
surprising that pass by value is used for arrays just like scalars. Objects are implicitly passed
by reference.
although php pass array just like scalars. but seems php create a new array and copy the element in
it to new array. it copy scalars like scalars but copy object as reference.
<?php
class Object {
public $x;
}
function array_inc_x($array) {
$array{'x'}->x++;
return $array;
}
$array = array();
$array['x'] = new Object();
$array['x']->x = 1;
$array2 = array_inc_x($array);
array_inc_x($array2);
# it would print 3, 3 here.
print $array['x']->x . ', ' . $array2['x']->x . "\n";
?>