PHP If语句在检查数组时不起作用

时间:2013-02-20 03:09:57

标签: php if-statement

我有这个If语句来检查用户提交的数据。首先将数据转换为数组,然后将其传递给此if语句所在的函数。

$errCount = 0;
$errMSG ='';
// Check Name
if ($submitData['FullName'] != '') {
    if (strlen ($submitData['FullName']) < 4) {
        $errCount + 1;
        $errMSG .='The Full Name was too short!';
    }
} else {
    $errCount + 1;
    $errMSG .='The Full Name Field was left blank!';
}

运行此操作时,即使使用空字符串,也不会触发任何错误。

我错过了什么吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

在递增时,您没有为$ errCount变量赋值;

$errCount = 0;
$errMSG ='';
$submitData['FullName']='';
// Check Name
if ($submitData['FullName'] != '') {

if (strlen ($submitData['FullName']) < 4) {
    $errCount=$errCount + 1; // or u can write  $errCount++;
    $errMSG .='The Full Name was too short!';
    }
} else {

 $errCount=$errCount + 1; // or u can write  $errCount++;
$errMSG .='The Full Name Field was left blank!';
}

echo $errMSG;

echo $errCount;

这将有效.....

答案 1 :(得分:0)

$errCount = 0;
$errMSG ='';
// Check Name

if(isset($submitData['FullName']) && strlen ($submitData['FullName'])>0 && strlen ($submitData['FullName'])<4)
{
    $errCount++;
    $errMSG .='The Full Name was too short!';
}
else if($submitData['FullName'] == '')
{
    $errCount++;
    $errMSG .='The Full Name Field was left blank!';
}
else
{
    //success
}