|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32192 preg_grep does not work with arrays containing references to themselves.
Submitted: 2005-03-04 17:23 UTC Modified: 2019-03-18 16:15 UTC
From: bolk at hitv dot ru Assigned: nikic (profile)
Status: Closed Package: PCRE related
PHP Version: 5.0.3 OS: Linux RH 9 and Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: bolk at hitv dot ru
New email:
PHP Version: OS:


 [2005-03-04 17:23 UTC] bolk at hitv dot ru
preg_grep damage GLOBALS

Bug in PHP 4.3.10 and 5.0.3

Reproduce code:
	preg_grep('/^T_/', $GLOBALS);

Expected result:
array(16) {
  array(1) {
    string(5) "a.php"
  array(0) {
  array(0) {
  array(0) {
  array(0) {
  array(0) {
  array(0) {


Actual result:
string(5) "Array"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-04 18:22 UTC]
That's because $GLOBALS contains reference to itself.
So I'd call this kinda expected behaviour.
See this code:
$a = Array();
$a[] = &$a; 

preg_grep('/^T_/', $a);
 [2005-03-04 18:36 UTC] bolk at hitv dot ru
Is it "expected behaviour" too? I think no.

	$b = array(1,2,3);

	$a = array();
	$a[] = &$b;

	preg_grep('/^T_/', $a);


array(1) {
  &string(5) "Array"
string(5) "Array"
 [2005-03-04 18:37 UTC] plintus at smtp dot ru
Tony, if a bug occurs in some other situations, it's still a bug. However, Microsoft can use this trick: "Bugs are expected behaviour in Windows" :-)
 [2005-03-04 18:56 UTC]
yup, this is the very same behaviour.
all elements of the target array get converted to strings, that's what you see.

propose your own solution instead of useless comments?
recursive references should be carefully used and current situation looks like a user fault to me.
 [2005-03-04 19:01 UTC] bolk at hitv dot ru
I belive this situation must be detected. I think it is a bug. preg_grep() should not change its agruments.
 [2005-03-04 19:08 UTC] bolk at hitv dot ru
I see that the zend_print_zval_r_ex() detects recursive references.
 [2005-03-04 19:18 UTC] bolk at hitv dot ru
Why not introduce a function that converts refs to non-refs in array? Say, array_flatten ($array) or something?
 [2016-12-30 22:54 UTC]
-Package: Feature/Change Request +Package: PCRE related
 [2019-03-18 16:15 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-03-18 16:15 UTC]
Looks like this has been fixed a long time ago:
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 27 01:03:34 2021 UTC