1

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

3
  • Your param list doesn't have any default values, so in order to call this function you must supply 4 parameters no matter what. Data validation on the other hand is still important. Commented May 27, 2011 at 0:12
  • 1
    @Chris Well, all it'll do is issue a warning, you're still not forced to pass all 4 parameters. Commented May 27, 2011 at 0:26
  • 1
    @deceze Bah, it's only a warning? Silly PHP. Commented May 27, 2011 at 0:28

7 Answers 7

2
if(count(array_filter($array)) == 0)
//   all values are empty 
2
  • You should expand your answer to explain how this satisfies the question, and provide a more relevant example of how it could be used in the context of the question. Commented Sep 22, 2012 at 23:47
  • here is an example for this answer : Commented Sep 23, 2012 at 10:31
1

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');
1

Maybe right but not best.
If you need to check all variables:

if (empty($username) || empty($password) || empty($fname) || empty($lname)) return false;
3
  • I've got number of functions that are similar and need to check if the values have been passed/set. So i don't really want to check each variable by it self, would much rather assign them to an array and do a check like in my post. Was just wondering if it the best way to go about it? Or there is an alternative way(without checking each variable)? Commented May 27, 2011 at 0:14
  • 1
    It's more looks like worst way. Do you want or not - it's not a question. If you have to check - you will check. Each variable. Commented May 27, 2011 at 0:21
  • 1
    This is exactly what array_filter does, but you have chosen the dirty way to do that. Commented May 27, 2011 at 0:34
1

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.

1

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);
    }
}
1

This won't check the validity of the data but:

// Do something if 4 (all) arguments were passed in
if(func_num_args() == 4) {
}
1

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.