简体   繁体   中英

php function inside another function

I have a function and I am trying to call that inside another function. i haven't done much php, so please clarify my question.

public function user($user)
{
if($user==''){
$this->logerror('not valid'); //this stores error in the db
return false;
}
}

I want to call the above function inside one main function, if I just do this way

public function main($username, $email) {
$this -> user($username);
//more code here to do something only if the user name is not empty
}

If i just do the main function like this, it stores the error saying "not valid" but the function continues. I understand that i am not returning anything so it does both the things, so how to stop the function if it's not valid username.

but if i just copy the content of the "user" function it works fine as it returns false. I assumed that calling the function user inside the main will do the same thing.

please advise.

regards

Just catch the return value and then do something with it

$result = $this->user($username);
if ($result === false) {
  return false; // or something else
} else {
  // Do something, if user() does not return 'false'
}

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