0

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

1 Answer 1

2

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'
}
Sign up to request clarification or add additional context in comments.

12 Comments

The user() method should be modified to return true if valid, for consistency's sake
Good point. However, I dont see any reason for the whole method, if in main() one can just use if($user !== '') { /* do something */ } else { $this->logerror('not valid'); }.
@kingcrunch, thanks for the quick reply, i tired what you said and it works fine.thanks. i kind of did that by assigning it to a variable, but what is the purpose of having the condition check in the separate function "user" already..also, if($result ==== false) can return false only right? what can be something else? can i echo something? sorry by the time i posted there are comments. will read those.
@kincrunch, that user function will be called in other places. earlier i did by doing it directly inside the main function
@gan: if $result is false, $result === false is true. Dont know, what you mean by "echo something". Of course you can echo something. If you want to see the value of a variable, use var_dump().
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.