Here is a simple code for user input information, the first if’s condition is
this->sanitize, to check the data is valid or not, another is
this->haveDuplicateUser_nameAndEmail(), which is use to check the user name and email is existed in the db or not.
the third one is
this->addNewUser(), which add the user record to the db.
if(!$this->sanitize()){
$this->printError(); //the data is not sanitize
return;
}else{
if($this->haveDuplicateUser_nameAndEmail()){ //duplicateUserNameAndPassword, cannot add new user
$this->printError();
}else{
if($this->addNewUser()){
$this->printSuccess(); //add user success
}else{
$this->printError(); //add user fail
}
}
}
You can use exceptions to simplify the block you have presented to us. You will have to update the code of the respective methods to throw these exceptions based on their own internal, boolean logic. There is no non-
ifsolution to say something like “Is this POST equal to an empty string?” in php.If you do this, you are getting into the realm of using exceptions as
gotos, which is generally frowned upon. I think you can debate it either way.