2013-06-12 184 views
2
if ($fname <> "" and $lname <> "" and $theemail <> "" and $empid <> "" and $doh <> "" and $ydept <> "#") { 
    if ($percentage >= 85) { 
     mail ($myEmail, $mailSubject, $msgBody, $header); 
     mail ($userEmail, $sentMailSubject, $sentMailBody, $sentHeader); 
     $filename = "output_annualexam.txt"; #Must CHMOD to 666, set folder to 777 
     $text = "\n" . str_pad($fname, 25) . "" . str_pad($lname, 25) . "" . str_pad($empID, 15) . "" . str_pad($doh, 15) . "" . str_pad($tdate, 20) . "" . str_pad($ydept, 44) . "" . str_pad($percentage, 0) . "%"; 

     $fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist) 
     if ($fp) { 
      fwrite ($fp, $text); 
      fclose ($fp); 
      #echo ("File written"); 
     } 
     else { 
      #echo ("File was not written"); 
     } 
     header ("Location: congrats.php?fname=$fname&type=$type"); 
    } 
    else { 
     header ("Location: nopass.php?percent=$percentage&type=$type"); 
    } 
} 
else { 
    header ("Location: tryagain.php?type=$type"); 
} 

檢查所有條件當$ ydept ==「#」這意味着用戶沒有從選擇的選項,這將使IF語句失敗,這應該自動把用戶的任何部門到tryagain.php頁面,而是將它們帶到nopass.php頁面。所以它失敗了。IF語句未PHP

我應該嘗試& &,而不是?但我認爲它不應該有所作爲。

+5

它可以使因爲[運算符優先級(HTTP有所不同:/ /www.php.net/manual/en/language.operators.precedence.php),其中''&&具有比'和'的優先級高。 – ajp15243

+1

但不應該檢查所有條件? – Si8

+1

嘗試'的var_dump($ ydept)'看看會發生什麼。 – Ryan

回答

4

是的,你應該使用&&AND

因爲我沒有足夠的說服力的解釋了吧,這裏有關於它的好帖子,感謝評論員)

'AND' vs '&&' as operator

+4

它在PHP事情做。當您使用&&時,它會評估整個表達式。當你使用AND時,它停在變量上。看到一個鏈接到的答案:http://stackoverflow.com/a/2803576/2360157 - 我的團隊剛剛從我們的應用程序中刪除並解決了以前的程序員不知道的幾個邏輯錯誤,因爲他們不知道&& vs AND – Jessica

+0

我會改變並現在測試...謝謝! – Si8

+0

我改爲&&和它仍然會NOPASS.php – Si8