Bug #1540: strpos(): returning FALSE or 0 is the same

From: Date: Tue, 15 Jun 1999 02:41:49 +0000
Subject: Bug #1540: strpos(): returning FALSE or 0 is the same
Groups: php.dev 
Request: Send a blank email to php-dev+get-6978@lists.php.net to get a copy of this message
From: a.suatoni@itaca.it Operating system: Digital Unix 3.2D PHP version: 3.0.9 PHP Bug Type: Misbehaving function Bug description: strpos(): returning FALSE or 0 is the same When using strpos() there is no way to differentiate between a "not found" condition and a return of 0 index. According to bug #1234 [1999-03-14 14:26:35] rasmus FALSE is defined as the empty string "" which strpos() will return when a match is not found. If a match is found in the first position it will return 0. According to bug #1323 [1999-04-16 16:03:08] rasmus Untrue. false is "" which is different from a string index of 0. You just have to check it correctly. However, the following code snippet demonstrates that there is no straight way to determine the real result of strpos(). ---- <?php echo "Test 1\n"; if (strpos("abcd", "e") == FALSE) echo "it should enter here\n"; else echo "it should NOT enter here\n"; echo "Test 2\n"; if (strpos("abcd", "a") == FALSE) echo "it should NOT enter here\n"; else echo "it should enter here\n"; ?> ---- The above code, when interpreted with "php -q < snippet", produces the following output: # php -q < snippet Test 1 it should enter here Test 2 it should NOT enter here ---- I've noted that strstr(), since it always returns a string, does not suffer the same problem as above, and it can be used as a temporary workaround in case one just wants to check if the needle is contained in the haystack. The problem has been noted also in previous versions of PHP3.

« previous php.dev (#6978) next »