i am retrieving the parameters using $_REQUEST. Is there a way of finding total no. of parameters in URL instead of retrieving each one and then counting ?
Asked
Active
Viewed 1.2k times
6
-
2what abount count($_GET) count($_POST) or count($_REQUEST) isnt working for you? – x4rf41 Jan 18 '13 at 11:43
4 Answers
7
This will give you the total number of &
separated URL query parameters:
count(explode('&', $_SERVER['QUERY_STRING']))
If you only want unique parameters, use $_GET
instead:
count($_GET)

Gumbo
- 643,351
- 109
- 780
- 844
-
thanks. on the basis of your idea i used count($_REQUEST) that worked too. – Reena Parekh Jan 18 '13 at 12:03
-
@New_2_PHP [`$_REQUEST` does not just contain URL parameters but also POST parameters and cookies.](http://php.net/reserved.variables.request) – Gumbo Jan 18 '13 at 12:04
4
Retrieve them with $_GET
. This should be enough.
Example:
// url: index.php?a=1&b=2&c=3
echo count($_GET); // 3 params, $_GET['a'], $_GET['b'], $_GET['c']
Note: you can also pass arrays in url ( check here ), and the whole array is counted once.

Community
- 1
- 1

Vlad Preda
- 9,780
- 7
- 36
- 63
0
This will do the trick for you. Try this :
$total = count($_GET); echo $total;

Ravinder Singh
- 3,113
- 6
- 30
- 46
0
If you only want the parameters in the URL, it's better to use $_GET. Since $_REQEUST contains the contents of $_GET, $_POST and $_COOKIE. see php.net
And if you want to know the number of parameters, you can get count the number of parameters that are in the URL using this: count($_GET)
.

Tjoene
- 320
- 9
- 18