简体   繁体   中英

php empty() or isset() checking more than 1 variable

I use

if(!empty($_POST['login'])){
}

for checking that login is file is filled, it work when I checking 1 variable, but not working when I use

if(!empty($_POST['login'],$_POST['password'] )){
}

how to check 2 variables, I see that isset() too support only 1 variable

Use a logical and operation ( && ) along with two calls to empty() , like so:

if( !empty( $_POST['login']) && !empty( $_POST['password'])) {
    // $_POST['login'] and $_POST['password'] are both not empty
}

Try to use this:

function isempty(){ // Function checks if all of the given arguments are empty
    $empty = true;
    $numargs = func_num_args(); // get the number of given Arguments
    $arg_list = func_get_args(); // get the given Arguments

    for ($i = 0; $i < $numargs; $i++) {
        $empty = $empty && empty($arg_list[$i]);                      
    }
    return $empty;
}

You can call it like this: !isempty($_POST['login'], $_POST['password'])

I have not tested the code, but it should be fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM