This proposal is for the often called line like this:
$var = isset($_GET['var']) ? $_GET['var'] : '';
Only a shorter and imho a cleaner solution to get the same:
$var = varset($_GET['var']);
The implementation for this in PHP code is this:
# Arg 1 = the variable to check for existence
# Arg 2 = the default return value which is an empty string by default
function varset($var, $default = '')
return (isset($var) ? $var : $default);
However there is a slight issue with this approach. If notices are turned on
this code will generate a notice while i think it should not do that. But
perhaps this approach is "to short".
A slightly different implementation (and longer) prevents the notice:
# Arg 1 = the array in which a given key should be checked for existence
# Arg 2 = the key to check in the array from arg 1
# Arg 3 = the default return value which is an empty string by default
function varset($arr, $key, $default = '')
return (isset($arr[$key]) ? $arr[$key] : $default);
where the call would be:
$var = varset($_GET, 'var');
I personally like both ways...
My proposal is to make this function a core php function in PHP 5.4. The
added benifit is obvious. People can, with this, use a way shorter notation
to validate if a given array element exists. Right now that needs to be done
with a ternary, filter_var or some other method (there are quite a few ways
to check for existence).
I tried to look in the PHP source to see if i could make a patch to add this
but i couldn't find the function that defines the isset function (i wanted
to base it on that). So a pointer to the right location would be nice (along
with docs that tell me what i all need to do to implement a new php
function). If someone else wants to make the implementation, please be my
So, what do you think of this?