davetgreen
davetgreen

Reputation: 109

Trying to check $_GET for empty parameters

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.

Upvotes: 6

Views: 1093

Answers (4)

smithbh
smithbh

Reputation: 347

You can use the following code to define and set a list of variables to the value passed via $_GET if it exists, or to an empty string if either not present or has no value in the querystring:

$getvars = array();
$expectedvars = array(                    // define variables to be set from $_GET
  'var1', 'var2', 'var3', 'var4', 'var5',
);

foreach($_GET as $key => $value) {        // store all $_GET variables present
  $getvars[$key] = $value;                // in an associative array
}

foreach($expectedvars as $variable) {          // now create all vaariables,
  ${$variable} = (isset($getvars[$variable]))  // if value was passed in url
    ? $getvars[$variable]                      // then set to that value
    : '';                                      // otherwise set as empty string
}

Which will create variables $var1, $var2, $var3, $var4, and $var5 with the appropriate values.

Upvotes: 0

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

If im reading your question correctly your looking too:

Check the parameter tobe set and have a value, but also use a foreach to loop the $_GET for convenience.

Perhaps, make an array of the params your expecting, and then loop through that, and check if also $_GET contains the same key.

<?php 
$allowed = array('var1','var2','var3');
$error = array();

foreach($allowed as $key) {
    if(array_key_exists($key,$_GET)) {
        //Validate, value must contain t least 1 len
        if(strlen($_GET[$key]) >= 1){
            $$key = $_GET[$key];
        }else{
            //Assign an error if param has a blank value
            $error[$key] = $key." parameter must contain a value";
        }

    }else{
        //Assign an error if params not been passed to the url
        $error[$key] = $key." parameter not set";
    }
}


if(!empty($error)){
    //A Params missing or has no value, dont continue
    echo 'All parameters required!<br />';
    foreach($error as $value){
        echo '* '.$value.'<br />';
    }
}else{
    //No errors do something
    echo 'var1 is set: '.$var1;
    echo 'var2 is set: '.$var2;
    echo 'var3 is set: '.$var3;
}
?>

Upvotes: 0

Kermit
Kermit

Reputation: 34055

How about something like this?

<?php 
$expectedVars = array('var1', 'var2');

foreach($expectedVars as $key => $val) {
    if($_GET[$val] == "") {
        $proceed = 0;
    } else {
        $proceed = 1;
    }
}
?>

Upvotes: 0

Ry-
Ry-

Reputation: 224857

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.

Upvotes: 2

Related Questions