我想我會使用preg_match()with的結果(bool),但我不太確定。我覺得不清楚結果是不是true還是false。示例 1:if (((bool) preg_match($pattern, $string, $matches)) === true)示例 2:if (((bool) !preg_match($pattern, $string, $matches)) === true)示例 3:if (((bool) preg_match($pattern, $string, $matches)) === false)示例 4:if (((bool) !preg_match($pattern, $string, $matches)) === false)另一種想法是:有結果的事情將來也安全嗎0?1你有這方面的經驗嗎?你能報告什么?編輯 0:鑒于if沒有比較運算符,問題得到了擴展。0總是false和1總是true?_示例 5:if ((preg_match($pattern, $string, $matches)))示例 6:if ((!preg_match($pattern, $string, $matches)))這個對嗎?(preg_match($pattern, $string, $matches))= 0| 1(!preg_match($pattern, $string, $matches))= true| false不是!
1 回答

30秒到達戰場
TA貢獻1828條經驗 獲得超6個贊
preg_match() 如果模式與給定主題匹配則返回 1,如果不匹配則返回 0,如果發生錯誤則返回 FALSE。這是 3 個可能的答案。如果將其簡化為布爾值 (true/false),則會丟失一些信息。
$result = (bool) preg_match($pattern, $string, $matches);
$result 如果模式匹配則為真,否則為假或發生錯誤。
此 if 條件僅在 preg_match 返回 1 時執行。
if (preg_match($pattern, $string, $matches)) {
}
如果不執行可能不匹配或出錯。
必須進行嚴格比較才能區分所有 3 種變體:
$preg_match_result = preg_match($pattern, $string, $matches);
if($preg_match_result === 1) {
//pattern matches
}
elseif($preg_match_result === 0) {
//pattern not matches
}
else {
//$preg_match_result === false
//an error occurred
}
- 1 回答
- 0 關注
- 90 瀏覽
添加回答
舉報
0/150
提交
取消