Currently, I check to see if a $_GET
or $_POST
property is set by using empty()
. Like this:
$status = null;
if (!empty($_GET['foo'])) {
$status = $_GET['foo'];
}
I imagine there is an even more concise way of doing the same thing built into PHP, that like what I'm doing now with empty
, also avoids printing the notice saying undefined index. Maybe something like this:
$status = something($_GET['foo']);
Or, maybe I should just ignore the notice and do:
$status = $_GET['foo'];
I'm not sure what the problem is as empty()
does not generate a warning for undefined variables, but if you want it in one line, you can use a ternary expression:
$status = empty($_GET['foo']) ? null : $_GET['foo'];
You can suppress the warnings by using @.
$status = @$_GET['foo'];
How about this if you want just GET:
$status = isset($_GET['foo'])?$_GET['foo']:NULL;
Or this if you want just GET and POST:
$status = isset($_GET['foo'])?$_GET['foo']:(isset($_POST['foo'])?NULL);
You can always check if the key in the $_GET array exists and set the variable to null if it isn't. Will never throw a notive.
$status = array_key_exists('foo', $_GET) ? $_GET['foo'] : null;