Multiple return statements in a PHP function

I have the following PHP function

function newUser($username, $password) {
     $checkUsernameAvailablity = check($username);
     if (!$checkUsernameAvailablity)
         {
           return -1;
         } 

         $checkPasswordComplexity = checkpass($password);
         if (!$checkPasswordComplexity)
         {
           return -2
         }

}

I would like to know if the username will be taken and the password is not complicated enough, whether PHP will stop the function after it returns -1 or continues, and also returns -2.

Thanks in advance RayQuang

+3
source share
3 answers
Operator

returnreturns a value and exits the function. If clicked return, no code will be executed in this body.

By the way, not all code paths have return values.

+3
source

When execution reaches the instruction return, the function will stop and return this value without further processing the function.

+6
source

.

, .

IF.

.

+1

Source: https://habr.com/ru/post/1775360/


All Articles