Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP returning an error message and false

I have a very simple PHP function to check log-in

function check_login($user, $pass) {
    if(!isset($user) || $user == '') {
        return  'Please enter a valid username';
    }
    else if(!isset($pass) || $pass == '') {
        return  'Please enter a valid password';
    }
    else {
        return 'true';
    }
}

How can I return an error message and false same time and return true instead of 'true' as a string. Like,

function check_login($user, $pass) {
    if(!isset($user) || $user == '') {
        return  'Please enter a valid username' //return false;
    }
    else if(!isset($pass) || $pass == '') {
        return  'Please enter a valid password' //return false;
    }
    else {
        return true;
    }
}

So I can check like if(check_login($uname, $pword)){ instead of checking like if(check_login($uname, $pword)=='true'){ :)

like image 573
madhushankarox Avatar asked Sep 28 '12 04:09

madhushankarox


1 Answers

Why not just check if it's true, and handle from there.

$login = check_login('user', 'pass');

if($login === true)
    loginUser(); //login was successful, finalize or whatever
else
    echo $login; //error message
like image 124
Austin Brunkhorst Avatar answered Sep 18 '22 17:09

Austin Brunkhorst