note 56661 added to function.unset
| From: | clark at everettsconsulting dot com | Date: | Sun, 11 Sep 2005 19:50:49 +0000 |
| Subject: | note 56661 added to function.unset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95021@lists.php.net to get a copy of this message | ||
In PHP 5.0.4, at least, one CAN unset array elements inside functions from arrays passed by
reference to the function.
As implied by the manual, however, one can't unset the entire array by passing it by reference.
<?php
function remove_variable (&$variable) // pass variable by reference
{
unset($variable);
}
function remove_element (&$array, $key) // pass array by reference
{
unset($array[$key]);
}
$scalar = 'Hello, there';
echo 'Value of $scalar is: ';
print_r ($scalar); echo '<br />';
// Value of $scalar is: Hello, there
remove_variable($scalar); // try to unset the variable
echo 'Value of $scalar is: ';
print_r ($scalar); echo '<br />';
// Value of $scalar is: Hello, there
$array = array('one' => 1, 'two' => 2, 'three' => 3);
echo 'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [two] => 2 [three] => 3 )
remove_variable($array); // try to unset the array
echo 'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [two] => 2 [three] => 3 )
remove_element($array, 'two'); // successfully remove an element from the array
echo 'Value of $array is: ';
print_r ($array); echo '<br />';
// Value of $array is: Array ( [one] => 1 [three] => 3 )
?>
----
Manual Page -- http://www.php.net/manual/en/function.unset.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56661
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56661&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56661&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56661&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56661&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56661&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56661&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56661&report=yes
Search -- http://master.php.net/manage/user-notes.php