# http_build_query unexpected/bad behaviour

- Thread: https://externals.io/message/10252
- Original archive: https://news-web.php.net/php.internals/10252
- Started by Jochem Maas on 2004-06-02T17:46:45+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #10252 — Jochem Maas — 2004-06-02T17:46:45+00:00

URL: https://externals.io/message/10252#10252

I came accross behaviour in `http_build_query()` which was thoroughly 
unexpected.

first a little background:
I am using `http_build_query()` which is turning out to be very useful for 
  putting together 'complex' URL 'pieces' (which are then again combined 
to form complete URLs - bare in mind that some of these pieces are 
passed along to javascripts so using `http_build_query()` once to form a 
complete URL query is not an option).

the issue is that the array $y below results in an empty string:

$x = array('e' => array('kf' => ''));
$y = array('e' => array('kf' => array()));

echo '\''. http_build_query($x). "'\n";
echo '\''. http_build_query($y) . "'\n";

running on: PHP 5.0.0RC3-dev (cli) (built: Jun  1 2004 18:04:30) (DEBUG)
----
would anybody care to comment as to whether this is what is intended?

kind regards,

Jochem Maas

---

## #10253 — Sara Golemon — 2004-06-02T20:43:04+00:00

URL: https://externals.io/message/10252#10253 · In reply to: #10252

> the issue is that the array $y below results in an empty string:
>
> $x = array('e' => array('kf' => ''));
> $y = array('e' => array('kf' => array()));
>
> echo '\''. http_build_query($x). "'\n";
> echo '\''. http_build_query($y) . "'\n";
>
> running on: PHP 5.0.0RC3-dev (cli) (built: Jun  1 2004 18:04:30) (DEBUG)
> ----
> would anybody care to comment as to whether this is what is intended?
>
I would definately call that intended.  What would you expect the resultant
query string to look like?

-Sara

---

## #10264 — Jochem Maas — 2004-06-03T17:04:24+00:00

URL: https://externals.io/message/10252#10264 · In reply to: #10253

Sara Golemon wrote:

> > […]
> 
> I would definately call that intended.  What would you expect the resultant
> query string to look like?

my original assumption was that both cases would produce the same
result. I don't see the rationale (although internally it may be
obvious) that the second array 'resolves' (is that the correct use of
the word?) into an empty string. -

an empty string is a valid value for a param coming from the POST/GET
request, php's loose nature lead me to assume the an empty array in such
a context would result in at least the some output - I went with the
feeling that an empty array is not emptier than an empty string.

anyway, thanks for the feedback.

> 
> -Sara

---

## #10265 — Sara Golemon — 2004-06-03T18:08:03+00:00

URL: https://externals.io/message/10252#10265 · In reply to: #10264

> > […]
> my original assumption was that both cases would produce the same
> result. I don't see the rationale (although internally it may be
> obvious) that the second array 'resolves' (is that the correct use of
> the word?) into an empty string. -
>
Let's look at them then:

$x becomes `e[kf]=`  which is to say "make e an array containing an empty
string at offset kf"

In the case of $y, the closest thing you could come to would be `e[kf][]=`
which says "make e an array containing another array at offset kf which
contains an empty string."  This is not precisely the same as what's
represented in the original $y variable.  If we were to process that query
string into variables we'd get back  array('e'=>array('kf'=>array('')))....
*close* but no cigar.

The problem is that array creation from a query string relies on implicit
array creation and there is no way to implicitly create an empty array.

> an empty string is a valid value for a param coming from the POST/GET
> request, php's loose nature lead me to assume the an empty array in such
> a context would result in at least the some output - I went with the
> feeling that an empty array is not emptier than an empty string.
>
It is in the sense that query strings don't honestly have a concept of
arrays.  So you can feed a query string an empty string and it says "Oh! An
empty string! How nice!"  but an empty array is so far outside of its
understand it'll just stare at you and wonder what's going through your
mind.

It's *possible* to change the behavior of `http_build_query()` to treat an
empty array as an array containing an empty string as its only element.  But
that is the sort of thing to lead to severely unexpected behavior and is
probably not the way to go.

-Sara

---

## #10267 — Jochem Maas — 2004-06-04T14:15:10+00:00

URL: https://externals.io/message/10252#10267 · In reply to: #10265

Sara, just a quick thankyou for taking the time to explain!
and to say that I will be adding some sanity checks around my use of 
 `http_build_query()` to avoid passing it arrays whose 'endpoints' are 
empty arrays as there is obviously nothing broken here

kind regards,
Jochem Maas

Sara Golemon wrote:

> > […]
> 
> Let's look at them then:
> 
> $x becomes `e[kf]=`  which is to say "make e an array containing an empty
> string at offset kf"
> 
> In the case of $y, the closest thing you could come to would be `e[kf][]=`
> which says "make e an array containing another array at offset kf which
> contains an empty string."  This is not precisely the same as what's
> represented in the original $y variable.  If we were to process that query
> string into variables we'd get back  array('e'=>array('kf'=>array('')))....
> *close* but no cigar.
> 
> The problem is that array creation from a query string relies on implicit
> array creation and there is no way to implicitly create an empty array.
> 
> 
> > […]
> 
> It is in the sense that query strings don't honestly have a concept of
> arrays.  So you can feed a query string an empty string and it says "Oh! An
> empty string! How nice!"  but an empty array is so far outside of its
> understand it'll just stare at you and wonder what's going through your
> mind.
> 
> It's *possible* to change the behavior of `http_build_query()` to treat an
> empty array as an array containing an empty string as its only element.  But
> that is the sort of thing to lead to severely unexpected behavior and is
> probably not the way to go.
> 
> -Sara
