Check for any variables passed to GET

I did a few searches and came up with nothing, I'm sure this is obvious.

Basically I am trying to work if something has been submitted via GET from the form.

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

Greetings

+5
source share
6 answers

Be careful when using count($_GET). If you submit a form with empty values, it will still create keys for the fields, and yours 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 not really empty:

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

echo count($_GET);                     // prints 2
echo count(array_filter($_GET, 'ne')); // prints 0
+8
source

This should complete the task:

if (!empty($_GET))
{
}
+7
source
if ( count($_GET) > 0 ) echo("I hear you!");
+1

if(empty($_GET)) { /* no parameters passed*/}

+1

$_GET count ($ _ GET). , 0

+1

Just "simple": if($_GET){ /* parameters passed*/}(for the current request) works to check if any query string was passed in the request GETor POST.

This is because an empty array is equal falsein a logical context if($x).
See: http://php.net/manual/en/types.comparisons.php

And indeed, there is no need for count()or empty().

0
source

All Articles