note 116562 added to functions.returning-values

From: Date: Thu, 22 Jan 2015 12:13:58 +0000
Subject: note 116562 added to functions.returning-values
Groups: php.notes 
Request: Send a blank email to php-notes+get-201499@lists.php.net to get a copy of this message
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"; ?> ---- Server IP: 72.52.91.14 Probable Submitter: 114.255.3.133 ---- Manual Page -- http://php.net/manual/en/functions.returning-values.php Edit -- https://master.php.net/note/edit/116562 Del: integrated -- https://master.php.net/note/delete/116562/integrated Del: useless -- https://master.php.net/note/delete/116562/useless Del: bad code -- https://master.php.net/note/delete/116562/bad+code Del: spam -- https://master.php.net/note/delete/116562/spam Del: non-english -- https://master.php.net/note/delete/116562/non-english Del: in docs -- https://master.php.net/note/delete/116562/in+docs Del: other reasons-- https://master.php.net/note/delete/116562 Reject -- https://master.php.net/note/reject/116562 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201499) next »