Search code examples
phpif-statementexit

What is the point of exit() after each if statement?


I've seen some php scripts that has exit() after each if statement. example:

if (empty($username)) {
   echo "error username is invalid";
  exit();
} else if (empty($password)) {
   echo "error password is invalid";
  exit();
} else {
  login($username, $password);
}

function login($u, $p)
{
   //do something here
   exit();
}

Solution

  • No, that is an absolutely pointless use of exit(). If you absolutely need one, just put it at the end of the script. Using exit() like that is only useful if you provide an exit code (used in command line applications).