8

If you try to read the value of a 'get' variable, what happens if said variable had not been set in the URL. Example: you request the page test.php, in that file it tries to read the value of $_GET['message']. What happens in this case? dose the value just get returned as ''?

Does this mean that if I am always expecting a value to be entered, and am not willing to accept a value of '' that I can just do something like

$foo = $_GET['bar'];
if($foo == ''){
  // Handle my 'error'
}
else
{
  // $foo should now have a value that I can work with
}

Please keep in mind I know that I could use isset($_GET['bar']) But I don't just want to know if it is set, I don't care if it is or not, I just care if it has a value that is more than just an empty string.

thecoshman
  • 8,394
  • 8
  • 55
  • 77

5 Answers5

7

If you don't care if the value is actually set, you can use this:

if (empty($_GET['bar']))
    // value is null, false, 0, '0' or an empty string (including whitespace).
elias
  • 1,481
  • 7
  • 13
  • I believe this is the perfect solution to my needs. So will this NOT generate an error if `$_GET['bar']` was not even set in the first place. – thecoshman Apr 19 '10 at 10:32
  • I think that it is important to point out though that this will consider the string `'0'` to be empty. If you where using this as an ID for pages to load this could cause you problems. I do believe though that most databases start auto-numbers for an ID column at 1 – thecoshman Apr 19 '10 at 10:48
  • yea, added that to the answer. – elias Apr 19 '10 at 10:53
6

If you try and access an array item that doesn't exist, the result will be null.

$foo = $_GET['bar']; //$foo is now null

It is worth noting that if you are using the weak comparison operator == rather than the strict comparsion ===, then '' will be treated as null.

The downside with the above code is that it will generate an notice when you access the array with an index that doesn't exist which is a bit messy.

Hence you should check if the value is set before you assign the contained value to a variable.

if ( !isset($_GET['bar']) ){
    //handle error or assign default value to $foo
}else{
    $foo = $_GET['bar'];
}

If you want to have a default value, and only replace that default value if the $_GET value exists you can use the conditional operator (scroll down to "Ternary Operator")

$foo = isset($_GET['bar']) ? $_GET['bar'] : 'default value';
Yacoby
  • 54,544
  • 15
  • 116
  • 120
1

Here is how I do it.

$foo = ( isset( $_GET['bar'] ) ) ? $_GET['bar'] : false;

if(false === $foo){
   die( 'no Foo for your Bar' );
}

As a side, you will probably never get a true Boolean value from $_GET

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
0

If I understand correctly it's:

$foo = $_GET['bar'];
if ((!isset($foo)) || $foo == '') {
//GET veriable not set, error
}
else {
// GET veriable set, code here.
}
Ali Demirci
  • 5,302
  • 7
  • 39
  • 66
0

Another way is to use PHP's filter_input function:

$foo = filter_input(INPUT_GET, 'bar');
if (empty($foo)) {
    // Handle my 'error'
} else {
    // $foo should now have a value that I can worth with
}
reformed
  • 4,505
  • 11
  • 62
  • 88