PHP 在 function() 中使用 exit() 到 return 一次只有 1 个值

PHP using exit() inside a function() to return only 1 value at a time

假设我编写了以下函数来检查空的发布值:

function checkEmpty($postValue, $msg){
  if($postValue == null){
    return alert_danger($msg);
    exit(); // NOT WORKING AS EXPECTED
  }
}

我正尝试通过这种方式为多个输入值调用它:

echo checkEmpty($city, "City cannot be empty");
echo checkEmpty($phone, "phone cannot be empty");
echo checkEmpty($name, "name cannot be empty");

现在,当我在上面调用此函数时,它会按应有的方式回显所有三个值,但我不希望这样。我希望它一次 return 1 个错误。如果 city 是空的,它应该只 return City cannot be empty 而不是其他的。当用户纠正错误后,它应该继续检查下一个错误。但是似乎函数内部使用的 exit() 没有按预期工作。我可以使用以下方法:

if($name == null){
  echo alert_danger("Please enter your Name.");
  exit();
}

if($email == null){
  echo alert_danger("Please enter your Email ID.");
  exit();
}

if($phone == null){
  echo alert_danger("Please enter your Phone number.");
  exit();
}

但是如果我有 15-20 个变量要检查的话,这会使代码不必要地太大。这就是为什么我想像之前尝试的那样在这里使用函数方法。解决方案应该是什么?

使用回显错误然后在以下时间结束的函数:

function checkEmpty($postValue, $msg){
    if($postValue == null){
        echo alert_danger($msg);
        exit();
    }
}

checkEmpty($city, "City cannot be empty"); //without echo