note 25127 deleted from function.strpos by didou

From: Date: Sat, 30 Oct 2004 18:06:03 +0000
Subject: note 25127 deleted from function.strpos by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79370@lists.php.net to get a copy of this message
Note Submitter: teddy at teddy dot com ---- In addition to cerium@x2productions.net: I always use the following function instead of strpos: function strpos2 ($s, $needle) { $pos = strpos($s, $needle); if ($pos === false) return -1; else return $pos; } So I can easily use it the following way: $pos = strpos2($string, $needle); if ($pos >= 0) { // found } else { // not found } or also possible: $pos = strpos2($string, $needle); for ($i = 0; $pos >= $i; $i++) { // do until position } Teddy Editor's Note: By testing explicitly for FALSE using the strict comparison operators (=== and !==), you can get the same result (and still be able to a faster built-in function :) i.e. $pos = strpos($string, $needle); if (FALSE !== $pos) { // found } else { // not found } ---- Reason: bad code

« previous php.notes (#79370) next »