Reputation: 3059
Is there a cleaner way to check for array values to prevent PHP notices? Currently I do this:
$email = (isset($_POST['user_email'])) ? $_POST['user_email'] : '';
$first_name = (isset($_POST['user_first_name'])) ? $_POST['user_first_name'] : '';
$last_name = (isset($_POST['user_last_name'])) ? $_POST['user_last_namel'] : '';
$birthday = (isset($_POST['user_last_name'])) ? $_POST['user_last_namel'] : '';
Is there a way to do something like JavaScript where you just provide a default, like this?
user.email = response.email || '';
I don't want to suppress notices, but these ugly checks clutter up my code. I'm on PHP 5.2.6.
Upvotes: 3
Views: 150
Reputation: 4905
You can use DefaultArray from NSPL:
$post = defaultarray('', $_POST);
$email = $post['user_email'];
$first_name = $post['user_first_name'];
$last_name = $post['user_last_name'];
$birthday = $post['user_birthday'];
Upvotes: 0
Reputation: 48897
You can create a function:
$email = getPost('user_email');
$first_name = getPost('user_first_name');
$last_name = getPost('user_last_name');
$birthday = getPost('user_birthday');
function getPost($key, $default = '')
{
if (isset($_POST[$key])) {
return $_POST[$key];
}
return $default;
}
Putting it in a function also let's you do additional sanitizing more easily, e.g., trim()
, if desired.
You can also pass a default value that will be returned:
$flavor = getPost('flavor', 'vanilla'); // if $_POST['flavor'] is not set, 'vanilla' is returned
Upvotes: 5
Reputation: 137320
I have seen at least three ways of doing this.
$options = $_POST + $defaults;
where $options
is an associative array having everything $_POST
had, but with added keys and values from $defaults
(unless there was a value in $_POST
for specific key in $defaults
, then this specific key/value pair from $defaults
is ignored and value for this specific key in $_POST
is not replaced).
As shown by @mario (use array_merge()
):
$options = array_merge($defaults, $_POST);
...such as Kohana's Arr::get()
helper:
/* ... */
public static function get($array, $key, $default = NULL)
{
return isset($array[$key]) ? $array[$key] : $default;
}
/* ... */
which has some advantage of easily replacing default value (which, by default is NULL
). It can be used like that:
echo Arr::get($_POST, 'user_email');
or:
echo Arr::get($_POST, 'user_email', 'N/A');
Upvotes: 2
Reputation: 145482
If you have a set of attributes, then one concise option is array_merge
. Note that the defaults go first:
$post = array_merge(
array("email" => "", "first_name" => "", "last_name" => "", "birthday" => ""),
$_POST
);
Then just access them as is:
$email = $post["email"]; // it's assured there is some value
An even more compact way to localize a limited set of variables is extract()
in combination with array_intersect_key()
. But only in that combination - to prevent importing arbitrary variables:
extract(array_merge($defaults, array_intersect_key(array_filter($_POST), $defaults)));
Upvotes: 3
Reputation: 65274
function assure(&$a, $default=null) {
if (!isset($a)) $a=$default;
return $a;
}
$email=assure($_POST['user_email'],'');
$first_name=assure($_POST['user_first_name'],'');
Upvotes: 0