php下使用strpos需要注意 === 運(yùn)算符
更新時(shí)間:2010年07月17日 23:54:42 作者:
首先應(yīng)該知道 strpos 函數(shù)可能返回布爾值 FALSE,但也可能返回一個(gè)與 FALSE 等值的非布爾值,例如 0 或者""。我們應(yīng)使用 === 運(yùn)算符來(lái)測(cè)試本函數(shù)的返回值。
復(fù)制代碼 代碼如下:
<?php
/*
判斷字符串是否存在的函數(shù)
*/
function strexists($haystack, $needle) {
return !(strpos($haystack, $needle) === FALSE);//注意這里的"==="
}
/*
Test
*/
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
// 簡(jiǎn)單的使用 "==" 號(hào)是不會(huì)起作用的,需要使用 "===",因?yàn)?a 第一次出現(xiàn)的位置為 0
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
// We can search for the character, ignoring anything before the offset
// 在搜索字符的時(shí)候可以使用參數(shù) offset 來(lái)指定偏移量
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?>
相關(guān)文章
php使用ZipArchive函數(shù)實(shí)現(xiàn)文件的壓縮與解壓縮
這篇文章主要介紹了php使用ZipArchive函數(shù)實(shí)現(xiàn)文件的壓縮與解壓縮,需要的朋友可以參考下2015-10-10
php 根據(jù)url自動(dòng)生成縮略圖并處理高并發(fā)問(wèn)題
服務(wù)器生成縮略圖的時(shí)機(jī)一般分為兩種:上傳文件時(shí)生成、訪問(wèn)時(shí)生成,下面為大家介紹下php根據(jù)url自動(dòng)生成縮略圖并處理高并發(fā)問(wèn)題2014-01-01
關(guān)于PHP二進(jìn)制流 逐bit的低位在前算法(詳解)
本篇文章是對(duì)PHP二進(jìn)制流逐bit的低位在前算法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP數(shù)組游標(biāo)實(shí)現(xiàn)對(duì)數(shù)組的各種操作詳解
這篇文章主要介紹了PHP數(shù)組游標(biāo)實(shí)現(xiàn)對(duì)數(shù)組的各種操作,結(jié)合實(shí)例形式較為詳細(xì)的分析了PHP數(shù)組操作中current與next方法控制數(shù)組游標(biāo)移動(dòng)實(shí)現(xiàn)數(shù)組遍歷的技巧,需要的朋友可以參考下2016-01-01

