|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2005-03-04 17:23 UTC] bolk at hitv dot ru
 Description:
------------
preg_grep damage GLOBALS
Bug in PHP 4.3.10 and 5.0.3
Reproduce code:
---------------
<?
	preg_grep('/^T_/', $GLOBALS);
	var_dump($GLOBALS);
?>
Expected result:
----------------
array(16) {
  ["argv"]=>
  array(1) {
    [0]=>
    string(5) "a.php"
  }
  ["argc"]=>
  int(1)
  ["HTTP_POST_VARS"]=>
  array(0) {
  }
  ["_POST"]=>
  array(0) {
  }
  ["HTTP_GET_VARS"]=>
  array(0) {
  }
  ["_GET"]=>
  array(0) {
  }
  ["HTTP_COOKIE_VARS"]=>
  array(0) {
  }
  ["_COOKIE"]=>
  array(0) {
  }
  ["HTTP_SERVER_VARS"]=>
etceteras
Actual result:
--------------
string(5) "Array"
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 12:00:01 2025 UTC | 
That's because $GLOBALS contains reference to itself. So I'd call this kinda expected behaviour. See this code: <?php $a = Array(); $a[] = &$a; preg_grep('/^T_/', $a); var_dump($a); ?>Is it "expected behaviour" too? I think no. <? $b = array(1,2,3); $a = array(); $a[] = &$b; preg_grep('/^T_/', $a); var_dump($a); var_dump($b); ?> array(1) { [0]=> &string(5) "Array" } string(5) "Array"