Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if any variables are passed in a GET

Tags:

php

I've done a few searches and not come up with anything, I'm sure it's obvious.

Basically I'm trying to work out if anything has been passed via GET from a form.

I know how to check for individual elements, but I just want to do a quick check if anything at all is passed

Cheers

like image 974
grant Avatar asked Aug 07 '10 22:08

grant


1 Answers

Be careful when using count($_GET). If you submit the form with empty values it will still create keys for the fields, and your count() will be greater than 0 and empty($_GET) will be false.

<?php 
print_r($_GET);
?>

<form action="" method="get">
    <input type="text" name="name">
    <textarea name="mytext"></textarea>
    <input type="submit">
</form>

Make sure the fields are actually not empty:

function ne($v) {
    return $v != '';
}

echo count($_GET);                     // prints 2
echo count(array_filter($_GET, 'ne')); // prints 0
like image 60
Aillyn Avatar answered Sep 23 '22 13:09

Aillyn