php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50407 http_build_query() on empty array
Submitted: 2009-12-08 08:31 UTC Modified: 2009-12-08 10:05 UTC
From: php at gimme dot org Assigned:
Status: Not a bug Package: URL related
PHP Version: 5.2.11 OS: Win
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at gimme dot org
New email:
PHP Version: OS:

 

 [2009-12-08 08:31 UTC] php at gimme dot org
Description:
------------
http_build_query() ignore the `key` in array( `key` => array() )..

Reproduce code:
---------------
<?php

$param = array(
	'_COOKIE' => array(),
	'_HEADER' => array(
		'User-Agent' => 'Mozilla...' ,
	),
);

echo http_build_query( $param );

?>

Expected result:
----------------
_COOKIE%5B0%D=&_HEADER%5BUser-Agent%5D=Mozilla...

Actual result:
--------------
_HEADER%5BUser-Agent%5D=Mozilla...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-08 10:05 UTC] jani@php.net
Not setting is same as setting it empty. No bug.
 [2010-10-06 13:42 UTC] adam dot zivner at gmail dot com
Applications often make distinction between empty and not set.

Example:

Set filter to '': http://example.com/articles/?filter=
Don't change filter, get it from cookies: http://example.com/articles/

But of course, this could be wrong way of using this. Is suggested explanation 
part of some standard/specification?
 [2012-02-17 21:23 UTC] ast dot derek at gmail dot com
Not set is not the same as empty:

http://example.com/?foo=&bar=2&hash=XXXX

$hash = $_GET['hash'];
unset($_GET['hash']);

if ($hash != hash(http_build_query($_GET))) {
    echo 'Hash mismatch';
    die;
}

Now we can't check the hash as http_build_query() won't return the

"URL-encoded query string from the associative (or indexed) array provided."

The original array is not used, a filtered copy was
processed instead.
 [2012-08-29 14:45 UTC] arnaud dot morin at gmail dot com
This is, IMHO, a poor design solution because we sometime want to provide a clear value to a parameter... as said before, empty is not the same as not provided...
 [2022-07-21 13:54 UTC] alestork78 at yahoo dot it
In 2022 this in still marked as NOT A BUG because someone did not understand the difference between "not set" and "empty"?

example.com?foo=bar (set value)
example.com?foo=    (set value to "empty")
example.com?        (do nothing)

The only solution to have things work like above is to rework this bad designed core php function every time? Won't be ever fixed?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC