note 45426 added to language.references.pass
| From: | SergioSantana:ssantana at tlaloc dot imta dot mx | Date: | Mon, 06 Sep 2004 22:55:21 +0000 |
| Subject: | note 45426 added to language.references.pass | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76117@lists.php.net to get a copy of this message | ||
Sometimes we need functions for building or modifying arrays whose elements are to be references to
other variables (arrays or objects for instance). In this example, I wrote two functions
'tst' and 'tst1' that perform this task. Note how the functions are written, and
how they are used.
<?
function tst(&$arr, $r) {
// The argument '$arr' is declared to be passed by reference,
// but '$r' is not;
// however, in the function's body, we use a reference to
// the '$r' argument
array_push($arr, &$r);
// Alternatively, this also could be $arr[] = &$r (in this case)
}
$arr0 = array(); // an empty array
$arr1 = array(1,2,3); // the array to be referenced in $arr0
// Note how we call the function:
tst($arr0, &$arr1); // We are passing a reference to '$arr1' in the call !
print_r($arr0); // $arr0 contains just the reference to $arr1
array_push($arr0, 5); // now we add another element to $arr0
array_push($arr1, 18); // and we add another element to $arr1 as well
print_r($arr1);
print_r($arr0); // Changes in $arr1 are reflected in $arr0
// -----------------------------------------
// A simpler way to do this:
function tst1(&$arr, &$r) {
// Both arguments '$arr' and '$r" are declared to be passed by
// reference,
// again, in the function's body, we use a reference to
// the '$r' argument
array_push($arr, &$r);
// Alternatively, this also could be $arr[] = &$r (in this case)
}
$arr0 = array(); // an empty array
$arr1 = array(1,2,3); // the array to be referenced in $arr0
// Note how we call the function:
tst1($arr0, $arr1); // 'tst1' understands '$r' is a reference to
'$arr1'
echo "-------- 2nd. alternative ------------ <br>\n";
print_r($arr0); // $arr0 contains just the reference to $arr1
array_push($arr0, 5); // now we add another element to $arr0
array_push($arr1, 18);
print_r($arr1);
print_r($arr0); // Changes in $arr1 are reflected in $arr0
?>
// This outputs:
// X-Powered-By: PHP/4.1.2
// Content-type: text/html
//
// Array
// (
// [0] => Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// )
//
// )
// Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// [3] => 18
// )
// Array
// (
// [0] => Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// [3] => 18
// )
//
// [1] => 5
// )
// -------- 2nd. alternative ------------
// Array
// (
// [0] => Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// )
//
// )
// Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// [3] => 18
// )
// Array
// (
// [0] => Array
// (
// [0] => 1
// [1] => 2
// [2] => 3
// [3] => 18
// )
//
// [1] => 5
// )
In both cases we get the same result.
I hope this is somehow useful
Sergio.
----
Manual Page -- http://www.php.net/manual/en/language.references.pass.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45426
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45426&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45426&report=yes
Search -- http://master.php.net/manage/user-notes.php