Home  >  Article  >  Backend Development  >  When using strpos under PHP, you need to pay attention to the === operator_PHP Tutorial

When using strpos under PHP, you need to pay attention to the === operator_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:001509browse

Copy code The code is as follows:

/*
Function to determine whether a string exists
*/
function strexists($haystack, $needle) {
return !(strpos($haystack, $needle) === FALSE);//Note the "==="
}
/*
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.
// Simple Using the "==" sign will not work, you need to use "===", because the first occurrence of a is 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
// You can use the offset parameter to specify the offset when searching for characters Amount
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322253.htmlTechArticleCopy the code as follows: ?php /* Function to determine whether a string exists*/ function strexists($haystack, $needle) { return !(strpos($haystack, $needle) === FALSE);//Note here...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn