2013-02-20 70 views
0

我有這個If語句來檢查用戶提交的數據。數據首先變成一個數組,然後傳遞給這個if語句所在的函數。PHP如果語句在檢查數組時不工作

$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!'; 
} 

運行此操作時,即使使用空字符串,也不會觸發任何錯誤。

我錯過了什麼嗎?

謝謝!

+0

錯誤計數保持爲0並沒有錯誤信息 - 即使有空白提交>。 – ShadowZzz 2013-02-20 03:11:01

+2

你沒有增加'$ errCount' ..'$ errCount + = 1;' – 2013-02-20 03:12:05

+0

'submitData'這是什麼改變到'$ _POST ['FullName']' – coolguy 2013-02-20 03:12:08

回答

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; 

這將工作.....

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 
}