I’ve got the following function:
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,
Pav
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.
To restrict it a bit more and accept, for example, empty strings and 0 as valid values, do something like:
The best way may be to accept an array, which you can explicitly test for the existence of keys, regardless of their values:
Regardless, this:
can be shortened to: