php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55650 !empty returns false positive if subkey not set, but not if _GET is set manually
Submitted: 2011-09-09 06:29 UTC Modified: 2011-09-12 19:51 UTC
From: chris dot wisefool at gmail dot com Assigned:
Status: Not a bug Package: Built-in web server
PHP Version: 5.3.8 OS: Windows 6.1 build 7600 i586
Private report: No CVE-ID: None
 [2011-09-09 06:29 UTC] chris dot wisefool at gmail dot com
Description:
------------
Running on PHP Version 5.3.5, EasyPHP Server Apache.
The test code on my system shows "SEARCH FILTER PARAM WAS PROVIDED" if ?search=1 
is passed for the query string but curiously not if ?search[a]=1 is provided. 
Based on some other bug reports (#27677) I thought maybe the behavior was correct, 
but if so then it seems a bug that when _GET is manually set to the same value 
instead of set by PHP parsing the query string, that it gives different results. 
So it seems a bug either way. :)


Test script:
---------------
<?php
# Assume http://somesite/?search=1
if (!empty($_GET['search']['filter'])) echo 'SEARCH FILTER PARAM WAS PROVIDED!';

# The REALLY odd thing is if I do this it does give the expected results:
$_GET 
if (!empty($_GET['search']['filter'])) 
   echo 'SEARCH FILTER PARAM WAS NOT PROVIDED!';

<?php
# The following (which obviously should be run as a separate script)
# demonstrates that the $_GET array seems the same in both cases 
# It seems like empty() is somehow seeing the _GET array as set by PHP
# and the manually set _GET array as different when containing identical 
# contents!
#  It seems very odd to me!
var_dump($_GET);
if (!empty($_GET['search']['filter'])) 
   echo 'SEARCH FILTER PARAM WAS NOT PROVIDED!';

$_GET = array('search'=>1);
var_dump($_GET);
if (!empty($_GET['search']['filter'])) 
   echo 'SEARCH FILTER PARAM WAS NOT PROVIDED!';


Expected result:
----------------
SEARCH FILTER PARAM WAS PROVIDED if ?search[filter]=1 provided
No "SEARCH FILTER PARAM WAS PROVIDED" if ?search=1

Actual result:
--------------
SEARCH FILTER PARAM WAS PROVIDED if ?search=1 or ?search[filter]=1 provided

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-09 06:39 UTC] chris dot wisefool at gmail dot com
Either the form munged part of my data (unlikely) or I pasted it in wrong. The 
line after 

if (!empty($_GET['search']['filter'])) echo 'SEARCH FILTER PARAM WAS PROVIDED!';

should be

$_GET = array('search'=>1);
if (!empty($_GET['search']['filter'])) 
   echo 'SEARCH FILTER PARAM WAS NOT PROVIDED!';

I'd edit it, but it seems you can't: you can only add comments...
 [2011-09-09 06:42 UTC] chris dot wisefool at gmail dot com
-Summary: !empty($_GET[$key][$subkey])) always returns false if $_GET[$key] is set +Summary: !empty returns false positive if subkey not set, but not if _GET is set manually
 [2011-09-09 06:42 UTC] chris dot wisefool at gmail dot com
Reworded bug summary to be more descriptive and succinct
 [2011-09-09 07:06 UTC] chris dot wisefool at gmail dot com
-Status: Open +Status: Closed
 [2011-09-09 07:06 UTC] chris dot wisefool at gmail dot com
After slightly changing my code example to:

<?php
var_dump($_GET);
if (!empty($_GET['search']['filter']))
   echo 'SEARCH FILTER PARAM WAS NOT PROVIDED!';

$_GET = array('search'=>'1');
var_dump($_GET);
if (!empty($_GET['search']['filter']))
   echo 'SEARCH FILTER PARAM WAS NOT PROVIDED!';

The results do seem consistent with the behavior as explained in #27677 (I see that the behavior didn't occur when set manually because I was setting 'search' to 1 NOT '1' and the subscripting behavior only occurred with strings. 

So it seems the behavior is correct per PHP's documentation, just not my assumptions based on that documentation. It means to test for a subkey you have to use the longer !empty($_GET['search']) && array_key_exists('filter', $_GET['search']) && is_array($_GET['search']['filter']), but hey, that's life :)

So I'm closing the bug.
 [2011-09-12 19:51 UTC] cataphract@php.net
-Status: Closed +Status: Bogus
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun May 26 11:01:26 2019 UTC