Operation of the die() function

Asked

Viewed 320 times

0

Using this code snippet as an example, the function die() could replace Return and Exit? It may have the same functioning as Return?

public function error()
{
    if(!$this->connection || !$this->statement) {
        return (object) array(
            'exception' => $this->connection->errno,
            'error' => $this->connection->error
        );
        exit;
    }
}
  • 2

    die and exit are synonyms and it cannot replace return, otherwise PHP would terminate the application instead of returning the value. Ps.: If you want to return the value, the exit is unnecessary in this method; if you want to terminate the application, the return would become unnecessary.

  • By the way exit will never be executed, as the return is out of function.

1 answer

1


No, it cannot replace the Return, the Return is the answer about what happened inside the function, because in home of exception you must treat it where called the function, an example is when something is false or true, when you call a function and it does not occur as the due, usually it returns false, ex:

 if(função){
    //Ocorreu com sucesso
 }else{
    //Deu algum erro
    die('Algum erro ocorreu!');
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.