Archive for June 3rd, 2008

03rd Jun 2008

PHP http_build_query

I was browsing through
PHP Cookbook : Solutions and Examples for PHP Programmers by David Sklar, Adam Trachtenberg the other day and while looking through the section on making REST calls, i noticed this function:


I have been doing PHP for some time and this was the first time I had ever seen it. I asked one of my co-workers this morning about it and he had never heard of it either. Here is what it does:

string http_build_query ( array $formdata [, string $numeric_prefix [, string $arg_separator ]] )

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

So in other words, instead of manually building a query string like:

$url = ‘’.$firstName.’&lastname=’.$lastname;

You can do something like this:

$queryString = http_build_query(array(‘firstname’=>$firstname, ‘lastname’=>$lastName));
$url = ‘’.$queryString;

This could be especially useful for long query strings so you don’t have to type out all of the string concatenation. It you would like to learn more about this method go here.

Posted by Posted by Jeremy Wischusen under Filed under PHP Comments No Comments »