http_build_query unexpected/bad behaviour

php.internals

Jochem Maas

22 years ago
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

Sara Golemon

22 years ago
> 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

Jochem Maas

22 years ago
Sara Golemon wrote:
>>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?
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 Golemon

22 years ago
> >>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"; > >> > 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

Jochem Maas

22 years ago
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: