【发布时间】:2019-01-01 16:00:31
【问题描述】:
我必须在 PHP 中评估一个很长的条件,因此,为了避免错误并尝试编写更具可读性的代码,我执行了以下操作:
//this returns 1 when true, and nothing when false, although expected TRUE or FALSE
$isNameValid=strlen($dataDecoded['nombre'])>=3;
$isDescriptionValid=(strlen($dataDecoded['descripcion'])>=10) && strlen($dataDecoded['descripcion'])<=300;
$isPriceValid=$dataDecoded['precio'] >0;
$isImageValid=(($dataDecoded['imagen'] != "") && ($dataDecoded['imagen'] != NULL) );
现在,我可以做到以下几点:
if($isNameValid==1 && $isDescriptionValid==1 && $isPriceValid==1 && $isImageValid==1)
{
echo "ok";
}
else{
echo "no";
}
它似乎工作正常,但可能是一种奇怪的做事方式。我想避免以下,我觉得更令人困惑和容易犯错误
if(strlen($dataDecoded['nombre'])>=3 && ... && ...)
有没有更好的方法来做到这一点?我做错了吗?谢谢
【问题讨论】:
标签: php conditional variable-assignment