#37761 [Opn]: Pass by reference function parameter passed as copy
| From: | hadrianoliver at hotmail dot com | Date: | Fri, 09 Jun 2006 11:19:21 +0000 |
| Subject: | #37761 [Opn]: Pass by reference function parameter passed as copy | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-98229@lists.php.net to get a copy of this message | ||
ID: 37761
User updated by: hadrianoliver at hotmail dot com
Reported By: hadrianoliver at hotmail dot com
Status: Open
Bug Type: Scripting Engine problem
Operating System: Windows XP Service Pack 2
PHP Version: 5CVS-2006-06-09 (snap)
New Comment:
//Correction: _array() function should be:
function &_array()
{
$a = array('a', 'b', 'c');
return $a;
}
Previous Comments:
------------------------------------------------------------------------
[2006-06-09 11:17:19] hadrianoliver at hotmail dot com
Description:
------------
When passing an argument to a function such as: -
preg_match( string pattern, string subject [, array &matches [, int
flags [, int offset]]] )
using the syntax:
preg_match($pattern, $subject, $array = array())
a copy of the $array is passed to the function, rather than a pointer.
Reproduce code:
---------------
// Without first defining variable
preg_match("/a/", "a", $matches1);
echo count($matches1); // Outputs 1 - OK
// Defining variable before function call
$matches2 = array();
preg_match("/a/", "a", $matches2);
echo count($matches2); // Outputs 1 - OK
// Defining variable within function parameters
preg_match("/a/", "a", $matches3 = array());
echo count($matches3); // Outputs 0 - UNEXPECTED!!
Expected result:
----------------
Expected result: 111
Actual result:
--------------
Observed result: 110
Further toying around, such as by using: -
function _array()
{
$a = array('a', 'b', 'c');
}
instead of array() and
function _preg_match($pattern, $subject, &$matches)
{
if (!$matches)
{
$digits = array();
}
echo "in: " . count($matches) ."<br/>\n";
$result = preg_match($pattern, $subject, $matches);
echo "out: " . count($matches) ."<br/>\n";
return $result;
}
instead of preg_match(), reveal that the array is being passed by copy,
not reference.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=37761&edit=1