Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP reading 'get' variable that may or may not have been set

Tags:

php

get

isset

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.

like image 393
thecoshman Avatar asked Apr 19 '10 10:04

thecoshman


1 Answers

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).
like image 54
elias Avatar answered Oct 24 '22 04:10

elias