How to grab URL parameters using PHP?

℡╲_俬逩灬. 提交于 2019-11-29 16:14:58

You don't need to do that manually, PHP already provides this functionality in the $_GET global variable:

<?php
    foreach($_GET as $key => $value)
        echo $key . " : " . $value;
?>

If it is a GET request, then all the params will be in $_GET. A form POST will be in $_POST. Both are contained in $_REQUEST.

You're looking for the $_GET superglobal

 foreach ($_GET as $key => $value) {
    echo $key . ' -- ' . $value;
}

You can access any $_GET values by using this code $_GET['sub1'] which will return sub-1

There is a much simpler way to do this rather than using a loop. Use the built in function parse_str(). It will split the request uri into key => value pairs. Example:

$url = "cat=3&sub1=sub-1&sub2=sub-2&sub3=sub-3&sub4=sub-4";
$query = array();
parse_str( $url, $query );
print_r($query);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!