php下使用strpos需要注意 運算子

2022-09-28 07:03:08 字數 739 閱讀 1258

複製** **如下:

php

/* 判斷字串是否存在的函式

*/ function st程式設計客棧rexists($haystack, $needle)

/* 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. 程式設計客棧

// 簡單的使用 "==" 號是不會起作用的,需要使用 "===",因為 a 第一次出現的位置為 0

if ($pos === false) else

// we can search for the character, ignoring anything before the offset

// 在搜尋字元的時候可以使用引數 offset 來指定偏移量

$newstring = 'abcdef abcdef';

$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0

?>

本文標題: php下使用strpos需要注意 === 運算子

本文位址:

使用 strpos 需要注意 運算子

首先應該知道 strpos 函式可能返回布林值 false,但也可能返回乙個與 false 等值的非布林值,例如 0 或者 我們應使用 運算子來測試本函式的返回值。判斷字串是否存在的函式 function strexists haystack,needle test mystring abc fin...

php 使用strpos 注意 運算子

strpos 函式可能返回布林值 false,但也可能返回乙個與 false 等值的非布林值,例如 0 或者 應使用 運算子來測試本函式的返回值。例子 判斷字串是否存在的函式 function strexists haystack,needle test www.jbxue.com mystring...

php使用strpos判斷字元是否存在

strpos string haystack,mixed needle int offset 0 int 返回 needle 在 haystack 中首次出現的數字位置。haystack 在該字串中進行查詢。needle 如果 needle 不是乙個字串,那麼它將被轉換為整型並被視為字元的順序值。o...