|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-06-09 11:17 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 06:00:01 2025 UTC |
//Correction: _array() function should be: function &_array() { $a = array('a', 'b', 'c'); return $a; }