Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to check $_GET for empty parameters

Tags:

php

I'm putting together a script that pulls through several $_GET variables which are then used within the script for the purposes of calculating a quote, etc.

The nightmare I'm having is simply being able to determine if any of them are without a value, for example ?var1=500&var2=&var3=Yes with var2 being the culprint there.

Depending on whether or not all of the $_GET variables have a value, or not, I'll take different actions accordingly.

I researched and came up with this as an option:

<?php 
foreach($_GET as $name => $value) {
    if ($value == "") {
        $proceed = 0;
    } else {
        $proceed = 1;
    }
}
?>

I'm echo'ing a simple bit of text using $proceed at the moment just for testing purposes.

This doesn't work, and I've considered isset and empty but I believe both options are useless in this case. I've read in a number of sources that $_GET parameters that aren't given values default to "' so I'm puzzled as to why this isn't working.

I can't use empty here due to the fact that sometimes the parameters will be set to 0.

It goes without saying that I've printed the contents of $_GET and get satisfactory results, so the data is all good.

Any help greatly appreciated.

like image 656
davetgreen Avatar asked Aug 30 '12 23:08

davetgreen


People also ask

How do you check if $_ GET is not empty?

<? php //Method 1 if(! empty($_GET)) echo "exist"; else echo "do not exist"; //Method 2 echo "<br>"; if($_GET) echo "exist"; else echo "do not exist"; //Method 3 if(count($_GET)) echo "exist"; else echo "do not exist"; ?>

Which function is used to check if $_ POST variable is empty?

The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

Does Isset check for empty string?

isset: Returns true for empty string, False, 0 or an undefined variable.

What is $_ GET variable?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL.


1 Answers

Missing parameters don't appear in $_GET. Say the querystring looks like this:

index.php?page=5

If you expected an id parameter, it's not automagically going to show up in $_GET. You just need to check using isset (and against an empty string) when you use them - not pre-emptively. That just doesn't work.

like image 150
Ry- Avatar answered Oct 04 '22 23:10

Ry-