note 123692 added to language.references.whatare
| From: | aldo dot caruso at argencasas dot com | Date: | Wed, 13 Mar 2019 16:50:47 +0000 |
| Subject: | note 123692 added to language.references.whatare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-210373@lists.php.net to get a copy of this message | ||
The following three code snippets show the effect of using references in scalar variables, arrays
and objects under different circumstances.
In any case the result is the expected one if you stick to the concept that a reference is an alias
to a variable. After assigning by reference ( no matter if $a =& $b or $b =& $a ) both
variable names refer to the same variable.
References with scalars
<?php
/*
References are aliases for the same variable
*/
$a = 1;
$b =& $a;
$b = 2;
echo "$a,$b\n"; // 2,2
$a = 3;
echo "$a,$b\n"; // 3,3
// Variables can be bound before being assigned
$c =& $d;
$c = 4;
echo "$c,$d\n"; // 4,4
?>
References with arrays
<?php
/*
Array elements referencing scalar variables
*/
$a = 1;
$b = 2;
$c = array(&$a, &$b);
$a = 3;
$b = 4;
echo "c: $c[0],$c[1]\n"; // 3,4
$c[0] = 5;
$c[1] = 6;
echo "a,b: $a,$b\n"; // 5,6
/*
Reference between arrays
*/
$d = array(1,2);
$e =& $d;
$d[0] = 3;
$d[1] = 4;
echo "e: $e[0],$e[1]\n"; // 3,4
$e[0] = 5;
$e[1] = 6;
echo "d: $d[0],$d[1]\n"; // 5,6
$e = 7;
echo "d: $d\n"; // 7 ( $d is no more an array, but an integer )
/*
Iterating an array of references using foreach construct
*/
$a = 1;
$b = 2;
$f = array(&$a,&$b);
foreach($f as $x) // If $x is assigned by value it doesn't change referred variables.
$x = 3;
echo "a,b: $a,$b\n"; // 1,2
foreach($f as &$x) // If $x is assigned by reference it changes referred variables.
$x = 3;
echo "a,b: $a,$b\n"; // 3,3
// Be aware that, after the loop, $x still references $f[1] and so $b
$x = 4;
echo "a,b: $a,$b\n"; // 3,4 ( $b affected )
// To avoid previous side effects it is advisable to unset x, unlinking it from $f[1] and $b
unset($x);
$x = 5;
echo "a,b: $a,$b\n"; // 3,4 ( $b not affected )
?>
References with objects
<?php
/*
Object property referencing a scalar variable
*/
$a = 1;
$b = new stdClass();
$b->x =& $a;
$a = 2;
echo "b->x: $b->x\n"; // 2
$b->x = 3;
echo "a: $a\n"; // 3
/* Reference between objects */
$c = new stdClass();
$c->x = 1;
$d =& $c;
$d->x = 2;
echo "c->x: $c->x\n"; // 2
$d = new stdClass();
$d->y = 3;
echo "c->y: $c->y\n"; // 3
echo "c->x: $c->x\n"; // Undefined property: stdClass::$x
?>
----
Server IP: 208.43.231.9
Probable Submitter: 82.199.157.34 (proxied: 181.47.208.104)
----
Manual Page -- http://php.net/manual/en/language.references.whatare.php
Edit -- https://master.php.net/note/edit/123692
Del: integrated -- https://master.php.net/note/delete/123692/integrated
Del: useless -- https://master.php.net/note/delete/123692/useless
Del: bad code -- https://master.php.net/note/delete/123692/bad+code
Del: spam -- https://master.php.net/note/delete/123692/spam
Del: non-english -- https://master.php.net/note/delete/123692/non-english
Del: in docs -- https://master.php.net/note/delete/123692/in+docs
Del: other reasons-- https://master.php.net/note/delete/123692
Reject -- https://master.php.net/note/reject/123692
Search -- https://master.php.net/manage/user-notes.php