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.
Â
⇒PHP Built-in Functions for Strings
⇒⇒PHP Tutorials
2016-10-13, 1787👍, 0💬
Popular Posts:
Where to find tutorials on OpenID? Here is a large collection of tutorials to answer many frequently...
How to view API details on the Publisher Portal of an Azure API Management Service 2017 version? You...
How to dump (or encode, serialize) a Python object into a JSON string using json.dumps()? The json.d...
How to use the "set-body" Policy Statement for an Azure API service operation? The "set-body" Policy...
FYIcenter.com Online Tools: FYIcenter JSON Validator and Formatter FYIcenter JSON to XML Converter F...