Function http_build_query in PHP4


According to the document of PHP official website, http://php.net/http_build_query

http_build_query

(PHP 5)

http_build_query — Generate URL-encoded query string

So, it’s only for PHP5+. In PHP4, we use this function below:

<?php

if (!function_exists('http_build_query')) {
    function http_build_query($data, $prefix = null, $sep = '', $key = '')
    {
        $ret = array();
        foreach ((array )$data as $k => $v) {
            $k = urlencode($k);
            if (is_int($k) && $prefix != null) {
                $k = $prefix . $k;
            }

            if (!empty($key)) {
                $k = $key . "[" . $k . "]";
            }

            if (is_array($v) || is_object($v)) {
                array_push($ret, http_build_query($v, "", $sep, $k));
            } else {
                array_push($ret, $k . "=" . urlencode($v));
            }
        }

        if (empty($sep)) {
            $sep = ini_get("arg_separator.output");
        }

        return implode($sep, $ret);
    }
}

?> 

Leave a Reply