最近在Greg Beaver's的blog上發表的一篇新文章 comparing strings in PHP with the == operator 中提及了PHP的 == 運算子在對字串進行比較時值得注意的問題。
在某些情況下,PHP會把類別數值資料(如含有數字的字串等)轉換成數值處理,== 運算子就是其中之一。在使用== 運算子對兩個字串進行鬆散比較時,PHP會把類別數值的字串轉換為數值進行比較,下面的實驗證實了這個結論:
<span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,187)"><code><span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,187)"><br><span style="FONT-FAMILY: Verdana"><?php</SPAN><BR style="FONT-FAMILY: Verdana"><SPAN style="FONT-FAMILY: Verdana">var_dump</SPAN></SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</SPAN><SPAN style="COLOR: rgb(221,0,0); FONT-FAMILY: Verdana">'01' </SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">== </SPAN><SPAN style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana">1</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">);<BR></SPAN><SPAN style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana"></SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana"><SPAN style="COLOR: rgb(0,0,187)"><SPAN style="FONT-FAMILY: Verdana">?></span><br><br></span></span>
var_dump('01' == 1);
?>
以上程式碼輸出結果為:
bool(true) 所以,在使用對字串進行比較時,建議使用=== 運算子對字串進行嚴格的檢查,或使用strcmp()等函數,從而避免可能產生的問題。 PHP手冊中的《PHP 類型比較表》對此也有詳細說明。 <span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana"><?php<BR>var_dump</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</SPAN><SPAN style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana">in_array</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</SPAN><SPAN style="COLOR: rgb(221,0,0); FONT-FAMILY: Verdana">'01'</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">, array(</SPAN><SPAN style="COLOR: rgb(221,0,0); FONT-FAMILY: Verdana">'1'</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">)));<BR></SPAN><SPAN style="COLOR: rgb(0,0,187)"><SPAN style="FONT-FAMILY: Verdana">?></span><br></span>
除此之外,常用的in_array()函數也存在弱類型的問題,見如下碼:<span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana"><?php <br>var_dump</span><span style=" COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</span><span style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana">in_array</span><span style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</span><span style="COLOR: rgb(221,0,0); FONT-FAMILY: Verdana">'01'<span style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">, array(</span><span style="COLOR: rgb(221,0,0); FONT-FAMILY : Verdana">'1'</span><span style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">)));<br></span><span style="COLOR: rgb(0,0,187)"><span style="FONT-FAMILY: Verdana">?></span></span></span></span>
以上代碼輸出結果為:bool(true)
相信用過該函數進行安全性檢查的PHP程式設計人員都知道這會產生怎麼樣的安全問題了吧?幸好<span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana"><?php <BR>var_dump</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</SPAN><SPAN style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana">in_array</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</SPAN><SPAN style="COLOR: rgb(221,0,0); FONT-FAMILY: Verdana">'01'</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">, array(</SPAN><SPAN style="COLOR: rgb(221,0,0); FONT-FAMILY: Verdana">'1'</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">), </SPAN><SPAN style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana">true</SPAN><SPAN style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">)); <BR></SPAN><SPAN style="COLOR: rgb(0,0,187)"><SPAN style="FONT-FAMILY: Verdana">?></span><br><br></span>
in_array()函數為我們提供了第三個參數,把它設為true 就可以開啟
in_array()函數的強制型別檢查機制,如下程式碼所示:<span style="COLOR: rgb(0,0,0)"><span style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana">var_dump</span><span style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</span><span style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana">in_array</span><span style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">(</span><span style="COLOR: rgb(221,0 ,0); FONT-FAMILY: Verdana">'01'</span><span style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">, array(</span><span style="COLOR: rgb(221,0,0); FONT-FAMILY: Verdana">'1'</span><span style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana">), <span style="COLOR: rgb(0,0,187); FONT-FAMILY: Verdana">true</span><span style="COLOR: rgb(0,119,0); FONT-FAMILY: Verdana"> )); </span><span style="COLOR: rgb(0,0,187)"><span style="FONT-FAMILY: Verdana">?></span></span></span></span>