Pav
Pav

Reputation: 2328

PHP check if array values are set

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

Upvotes: 1

Views: 2515

Answers (7)

ahmad jaberi
ahmad jaberi

Reputation: 37

if(count(array_filter($array)) == 0)
//   all values are empty 

Upvotes: 2

deceze
deceze

Reputation: 522085

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

Upvotes: 1

Adam Bergmark
Adam Bergmark

Reputation: 7536

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

Upvotes: 1

onteria_
onteria_

Reputation: 70497

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

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

Upvotes: 1

RRStoyanov
RRStoyanov

Reputation: 1172

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

Upvotes: 1

Jon
Jon

Reputation: 437376

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.

Upvotes: 1

user680786
user680786

Reputation:

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

if (empty($username) || empty($password) || empty($fname) || empty($lname)) return false;

Upvotes: 1

Related Questions