Trying to check $ _GET for empty parameters

I am compiling a script that pulls out several $ _GET variables, which are then used in the script for the purpose of calculating quotes, etc.

The nightmare I have is simply to determine if there is any of them without a value, for example: var1 = 500 & var2 = & var3 = Yes, where var2 is a consequence of this.

Depending on whether or not all $ _GET variables have or not, I will 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 typing plain text using $ continue at the moment for testing purposes only.

This does not work, and I considered isset and empty , but I believe that both options are useless in this case. I read in several sources that the $ _GET parameters, which are not set to default values, are "therefore puzzled by why this does not work.

I cannot use empty here because sometimes the parameters will be set to 0.

It goes without saying that I printed the contents of $ _GET and got satisfactory results, so the data is fine.

Any help is greatly appreciated.

+6
source share
4 answers

Missing options are not displayed in $_GET . Let's say the query looks like this:

 index.php?page=5 

If you were expecting an id parameter, it will not automatically appear in $_GET . You just need to check with isset (and against the empty string) when you use them, and not proactively. It just doesn't work.

+2
source

How about something like that?

 <?php $expectedVars = array('var1', 'var2'); foreach($expectedVars as $key => $val) { if($_GET[$val] == "") { $proceed = 0; } else { $proceed = 1; } } ?> 
0
source

If im reads your question correctly, you too look:

Check the parameter that should be set and make a difference, but also use foreach to use $ _GET cyclically for convenience.

Perhaps make an array of parameters waiting for you, and then skip this and check if $ _GET also 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; } ?> 
0
source

You can use the following code to define and set the list of variables to the value passed through $ _GET, if it exists, or an empty string if it is missing or does not matter in the query string:

 $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 the variables $ var1, $ var2, $ var3, $ var4 and $ var5 with the corresponding values.

0
source

Source: https://habr.com/ru/post/924225/


All Articles