PHP check if array values are set
I've got the following fun开发者_开发知识库ction:
public function insertMember($username, $password, $fname, $lname)
{
$param = array();
$param['username'] = $username;
$param['password'] = $password;
$param['fname'] = $fname;
$param['lname'] = $lname;
return (count(array_filter($param, 'strlen')) == 0) ? FALSE : $this->insertIntoDB($param);
}
Is using (count(array_filter($param, 'strlen')) == 0)
the right/best way to go about checking if all the variables $username, $password, $fname, $lname
have been passed to the function?
Thanks,
Pavif(count(array_filter($array)) == 0)
// all values are empty
You can't really check whether the variables have been passed to the function or not, you can only check their value. If the value is falsey, you may reject it. That doesn't necessarily mean that the variable wasn't passed, just that the value was falsey.
if (!$username || !$password || !$fname || !$lname)
To restrict it a bit more and accept, for example, empty strings and 0 as valid values, do something like:
public function insertMember($username = null, $password = null, $fname = null, $lname = null) {
if ($username === null || $password === null || $fname === null || $lname === null)
The best way may be to accept an array, which you can explicitly test for the existence of keys, regardless of their values:
public function insertMember($values) {
if (array_diff_key(array_flip(array('username', 'password', 'fname', 'lname')), $values)) {
// not all keys were set!
}
Regardless, this:
$param = array();
$param['username'] = $username;
$param['password'] = $password;
$param['fname'] = $fname;
$param['lname'] = $lname;
can be shortened to:
$params = compact('username', 'password', 'fname', 'lname');
Maybe right but not best.
If you need to check all variables:
if (empty($username) || empty($password) || empty($fname) || empty($lname)) return false;
You can omit the 'strlen'
parameter, the default behavior of array_filter
will work fine in this case. This way the code is just a tad shorter.
However, as a matter of style you may want to consider the explicit
if (empty($username) || empty($password) || ...)
because it more readily communicates to the reader what the requirements of the function are as regards its arguments.
Why not put your check even early? Make it something like that:
public function insertMember($username, $password, $fname, $lname) {
if (!$username || !$password || !$fname || !lname) {
return false;
} else {
$param = array();
$param['username'] = $username;
$param['password'] = $password;
$param['fname'] = $fname;
$param['lname'] = $lname;
return $this->insertIntoDB($param);
}
}
This won't check the validity of the data but:
// Do something if 4 (all) arguments were passed in
if(func_num_args() == 4) {
}
I prefer not using array_filter
since it feels kind of dirty to pass a function in by its name. Although if you're using stdlib functions it may be okay.
I would use a loop
foreach ($param as $v) {
if (empty($v)) return false;
}
精彩评论