Tools, FAQ, Tutorials:
Testing the strpos() Return Value in PHP
What Is the Best Way to Test the strpos() Return Value?
✍: FYIcenter.com
Because strpos() could two types of values, Integer and Boolean, you need to be careful about testing the return value. The best way is to use the "Identical(===)" operator. Do not use the "Equal(==)" operator, because it does not differentiate "0" and "false". Check out this PHP script on how to use strpos():
<?php
$haystack = "needle234953413434516504381640386488129";
$pos = strpos($haystack, "needle");
if ($pos==false) {
print("Not found based (==) test\n");
} else {
print("Found based (==) test\n");
}
if ($pos===false) {
print("Not found based (===) test\n");
} else {
print("Found based (===) test\n");
}
?>
This script will print:
Not found based (==) test Found based (===) test
Of course, (===) test is correct.
2016-10-13, ∼4485🔥, 0💬
Popular Posts:
Where to find tutorials on Visual Studio? I want to know How to learn Visual Studio. Here is a large...
What properties and functions are supported on http.client.HTTPResponse objects? If you get an http....
How to build a test service operation to dump everything from the "context.Request" object in the re...
What's Wrong with "while ($c=fgetc($f)) {}" in PHP? If you are using "while ($c=fgetc($f)) {}" to lo...
How to pull NVIDIA CUDA Docker Image with the "docker image pull nvidia/cuda" command? If you are ru...