http_build_query() in PHP5
2010 February 8
I can’t tell how many times I need to pass some data via GET method to another PHP script and I always forget about this neat little function so I just do it like this (like a fool):
-
$arr = array('foo' => 'bar',
-
'baz' => 'boom',
-
'cow' => 'milk');
-
$query = array();
-
foreach ($arr as $k => $v) {
-
$query[] = $k . '=' . $v;
-
}
-
$query = implode('&', $query);
-
// foo=bar&baz=boom&cow=milk
When it’s so easy with the http_build_query() function:
-
$query = http_build_query($arr);
-
// foo=bar&baz=boom&cow=milk
This is also useful when using cURL to perform POST or GET calls.
2 Responses
leave one →
Should it be join() instead of explode()?
It would also be cool to add urlencode() to that function I think.
It should have been implode(), not explode(). My bad.