Get URL query string parameters

前端 未结 11 1477
长情又很酷
长情又很酷 2020-11-22 17:41

What is the \"less code needed\" way to get parameters from a URL query string which is formatted like the following?

www.mysite.com/category/subcateg

11条回答
  •  甜味超标
    2020-11-22 17:47

    For getting each node in the URI, you can use function explode() to $_SERVER['REQUEST_URI']. If you want to get strings without knowing if it is passed or not. you may use the function I defined myself to get query parameters from $_REQUEST (as it works both for POST and GET params).

    function getv($key, $default = '', $data_type = '')
    {
        $param = (isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default);
    
        if (!is_array($param) && $data_type == 'int') {
            $param = intval($param);
        }
    
        return $param;
    }
    

    There might be some cases when we want to get query parameters converted into Integer type, so I added the third parameter to this function.

提交回复
热议问题