如何在PHP中减少用于表单验证的嵌套?

Dar*_*ill 0 php

这个函数占用了我的函数页面上的很多行.

除非将它们放入数组中,然后使用foreach循环,否则我无法弄清楚如何将其删除.但是我希望它具体说明不允许空白的内容.

if ($EmployeeID === "")
{
  echo "EmployeeID Is Blank.";

}
else
{
  if ($Firstname === "")
  {
    echo "Firstname Is Blank.";
  }
  else
  {
    if ($Lastname === "")
    {
      echo "Last Name Is Blank";

    }
    else
    {
      if ($PhoneNumber === "")
      {
        echo "Phone Number Is Blank";

      }
      else
      {
        if ($Address === "")
        {
          echo "Address Is Blank";

        }
        else
        {
          if ($City === "")
          {
            echo "City Is Blank";

          }
          else
          {
            if ($State === "")
            {
              echo "Sate Is Blank";

            }
            else
            {
              if ($Zip === "")
              {
                echo "Zip Is Blank";

              }
              else
              {
                if ($Email === "")
                {
                  echo "Email Is Blank";

                }
                else
                {
                  if ($Password === "")
                  {
                    echo "Password Is Blank";

                  }
                  else
                  {
                    echo "All Success";
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我找到的解决方案将线路关闭,但是没有准确地告诉我什么字段留空:

$Array = array();
$Array[] = $EmpID;
$Array[] = $FirstName;
$Array[] = $Lastname;
$Array[] = $PhoneNumber;
$Array[] = $Address;
$Array[] = $City;
$Array[] = $State;
$Array[] = $Zip;
$Array[] = $Email;
$Array[] = $Password;   
foreach ($Array AS $Checking) 
{
  if (empty($Checking))
  {
    echo "One Or More Is Left Blank"; 
    exit;
  }
}
Run Code Online (Sandbox Code Playgroud)

Sam*_*ook 5

try{
    if(empty($EmployeeID)){
        throw new Exception('EmployeeID Is Blank.');
    }elseif(empty($Firstname)){
        throw new Exception('Firstname Is Blank.');
    }elseif(empty($Lastname)){
        throw new Exception('Lastname Is Blank.');
    }elseif(empty($PhoneNumber)){
        throw new Exception('PhoneNumber Is Blank.');
    }elseif(empty($Address)){
        throw new Exception('Address Is Blank.');
    }elseif(empty($City)){
        throw new Exception('City Is Blank.');
    }elseif(empty($State)){
        throw new Exception('State Is Blank.');
    }elseif(empty($Zip)){
        throw new Exception('Zip Is Blank.');
    }elseif(empty($Email)){
        throw new Exception('Email Is Blank.');
    }elseif(empty($Password)){
        throw new Exception('Password Is Blank.');
    }
    echo 'All Success';
}catch(Exception $e){
    echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)