die();
在 PHP 中验证数据时过度使用该函数是不好的编程习惯吗?
我正在构建一个已经使用该函数十多次的类,因此如果验证测试失败,整个脚本就会死掉。这是一个好的做法,有没有更好的方法我应该这样做?这种做法是否因语言而异,是否有任何编程范围的约定或最佳实践可供学习?
这是一个例子:
if($error = $this->checkDate($start)){
echo "START: ".$error."\r\n";
die();
}
if($error = $this->checkDate($end)){
echo "END: ".$error."\r\n";
die();
}
if($start>$end){
$error = "The start date must come <i>after</i> the end date!";
echo $error;
die();
}
if(($end-$start)<(3*24*3600) || ($end-$start)>(3*30*24*3600)){
$error = "The period must be at <i>least</i> 3 days long, and at <i>most</i> 3 months long. This date spans about ".round(($end-$start)/(3600*24*30))." months.";
echo $error;
die();
}
等等等等等等……
PS-“最佳实践”怎么还不是标签?