11<?xml version =" 1.0" encoding =" utf-8" ?>
22<!-- $Revision$ -->
3- <chapter xml : id =" language.references" xmlns =" http://docbook.org/ns/docbook" >
3+ <chapter xml : id =" language.references" xmlns =" http://docbook.org/ns/docbook" annotations = " interactive " >
44 <title >References Explained</title >
55
66 <sect1 xml : id =" language.references.whatare" >
3838 In the first of these, PHP references allow you to make two
3939 variables refer to the same content. Meaning, when you do:
4040 <informalexample >
41- <programlisting role =" php" >
41+ <programlisting role =" php" annotations = " non-interactive " >
4242<![CDATA[
4343<?php
44-
4544$a =& $b;
46-
47- ?>
4845]]>
4946 </programlisting >
5047 </informalexample >
@@ -81,8 +78,6 @@ var_dump(array_key_exists('b', $b)); // bool(true)
8178$c = new stdClass();
8279foo($c->d);
8380var_dump(property_exists($c, 'd')); // bool(true)
84-
85- ?>
8681]]>
8782 </programlisting >
8883 </example >
@@ -92,13 +87,10 @@ var_dump(property_exists($c, 'd')); // bool(true)
9287 The same syntax can be used with functions that return
9388 references:
9489 <informalexample >
95- <programlisting role =" php" >
90+ <programlisting role =" php" annotations = " non-interactive " >
9691<![CDATA[
9792<?php
98-
9993$foo =& find_var($bar);
100-
101- ?>
10294]]>
10395 </programlisting >
10496 </informalexample >
@@ -120,7 +112,6 @@ $foo =& find_var($bar);
120112 <programlisting role =" php" >
121113<![CDATA[
122114<?php
123-
124115$var1 = "Example variable";
125116$var2 = "";
126117
@@ -140,8 +131,6 @@ echo "var2 is set to '$var2'\n"; // var2 is set to ''
140131
141132global_references(true);
142133echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
143-
144- ?>
145134]]>
146135 </programlisting >
147136 </example >
@@ -159,7 +148,6 @@ echo "var2 is set to '$var2'\n"; // var2 is set to 'Example variable'
159148 <programlisting role =" php" >
160149<![CDATA[
161150<?php
162-
163151$ref = 0;
164152$row =& $ref;
165153
@@ -168,8 +156,6 @@ foreach (array(1, 2, 3) as $row) {
168156}
169157
170158echo $ref; // 3 - last element of the iterated array
171-
172- ?>
173159]]>
174160 </programlisting >
175161 </example >
@@ -185,7 +171,6 @@ echo $ref; // 3 - last element of the iterated array
185171 <programlisting role =" php" >
186172<![CDATA[
187173<?php
188-
189174$a = 1;
190175$b = array(2, 3);
191176
@@ -194,8 +179,8 @@ $arr[0]++;
194179$arr[1]++;
195180$arr[2]++;
196181/* $a == 2, $b == array(3, 4); */
197-
198- ?>
182+ var_dump($a);
183+ var_dump($b);
199184]]>
200185 </programlisting >
201186 </informalexample >
@@ -216,6 +201,7 @@ $a = 1;
216201$b =& $a;
217202$c = $b;
218203$c = 7; // $c is not a reference; no change to $a or $b
204+ print "a = {$a}; b = {$b}; c = {$c}\n\n";
219205
220206/* Assignment of array variables */
221207$arr = array(1);
@@ -224,8 +210,9 @@ $arr2 = $arr; // Not an assignment-by-reference!
224210$arr2[0]++;
225211/* $a == 2, $arr == array(2) */
226212/* The contents of $arr are changed even though it's not a reference! */
227-
228- ?>
213+ print "a = {$a}\n";
214+ var_dump($arr);
215+ var_dump($arr2);
229216]]>
230217 </programlisting >
231218 </informalexample >
@@ -245,16 +232,14 @@ $arr2[0]++;
245232 <programlisting role =" php" >
246233<![CDATA[
247234<?php
248-
249235function foo(&$var)
250236{
251237 $var++;
252238}
253239
254- $a= 5;
240+ $a = 5;
255241foo($a);
256-
257- ?>
242+ print $a;
258243]]>
259244 </programlisting >
260245 </informalexample >
@@ -281,18 +266,15 @@ foo($a);
281266 As said before, references are not pointers. That means, the
282267 following construct won't do what you expect:
283268 <informalexample >
284- <programlisting role =" php" >
269+ <programlisting role =" php" annotations = " non-interactive " >
285270<![CDATA[
286271<?php
287-
288272function foo(&$var)
289273{
290274 $var =& $GLOBALS["baz"];
291275}
292276
293277foo($bar);
294-
295- ?>
296278]]>
297279 </programlisting >
298280 </informalexample >
@@ -322,18 +304,14 @@ foo($bar);
322304 <programlisting role =" php" >
323305<![CDATA[
324306<?php
325-
326307function foo(&$var)
327308{
328309 $var++;
329310}
330311
331- $a=5;
332-
312+ $a = 5;
333313foo($a);
334- // $a is 6 here
335-
336- ?>
314+ print $a; // $a is 6 here
337315]]>
338316 </programlisting >
339317 </informalexample >
@@ -364,6 +342,7 @@ foo($a);
364342function foo(&$var)
365343{
366344 $var++;
345+ print $var;
367346}
368347
369348function &bar()
@@ -373,8 +352,6 @@ function &bar()
373352}
374353
375354foo(bar());
376-
377- ?>
378355]]>
379356 </programlisting >
380357 </informalexample >
@@ -389,7 +366,7 @@ foo(bar());
389366 result is undefined. For example, the following examples of passing
390367 by reference are invalid:
391368 <informalexample >
392- <programlisting role =" php" >
369+ <programlisting role =" php" annotations = " non-interactive " >
393370<![CDATA[
394371<?php
395372
@@ -413,8 +390,6 @@ class Foobar {}
413390
414391foo(new Foobar()) // Produces a notice as of PHP 7.0.7
415392 // Notice: Only variables should be passed by reference
416-
417- ?>
418393]]>
419394 </programlisting >
420395 </informalexample >
@@ -449,8 +424,6 @@ $obj = new Foo();
449424$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42
450425$obj->value = 2;
451426echo $myValue; // Prints the new value of $obj->value, i.e. 2
452-
453- ?>
454427]]>
455428 </programlisting >
456429 </informalexample >
@@ -495,14 +468,17 @@ $collection = &collector();
495468$collection[] = 'foo';
496469
497470print_r(collector());
498- // Array
499- // (
500- // [0] => foo
501- // )
502-
503- ?>
504471]]>
505472 </programlisting >
473+ &example.outputs;
474+ <screen >
475+ <![CDATA[
476+ Array
477+ (
478+ [0] => foo
479+ )
480+ ]]>
481+ </screen >
506482 </informalexample >
507483 <note >
508484 <simpara >
@@ -513,7 +489,7 @@ print_r(collector());
513489 To pass the returned reference to another function expecting a reference
514490 you can use this syntax:
515491 <informalexample >
516- <programlisting role =" php" >
492+ <programlisting role =" php" annotations = " non-interactive " >
517493<![CDATA[
518494<?php
519495
@@ -524,8 +500,6 @@ function &collector()
524500}
525501
526502array_push(collector(), 'foo');
527-
528- ?>
529503]]>
530504 </programlisting >
531505 </informalexample >
@@ -552,8 +526,8 @@ array_push(collector(), 'foo');
552526$a = 1;
553527$b =& $a;
554528unset($a);
555-
556- ?>
529+ var_dump($a);
530+ var_dump($b);
557531]]>
558532 </programlisting >
559533 </informalexample >
@@ -582,13 +556,10 @@ unset($a);
582556 are in fact creating reference to a global variable. That means,
583557 this is the same as:
584558 <informalexample >
585- <programlisting role =" php" >
559+ <programlisting role =" php" annotations = " non-interactive " >
586560<![CDATA[
587561<?php
588-
589562$var =& $GLOBALS["var"];
590-
591- ?>
592563]]>
593564 </programlisting >
594565 </informalexample >
0 commit comments