Which is the best way to test the strpos() Return Value in PHP?
Submitted by: AdministratorBecause 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 ");
} else {
print("Found based (==) test ");
}
if ($pos===false) {
print("Not found based (===) test ");
} else {
print("Found based (===) test ");
}
?>
This script will print:
Not found based (==) test
Found based (===) test
Of course, (===) test is correct.
Submitted by:
<?php
$haystack = "needle234953413434516504381640386488129";
$pos = strpos($haystack, "needle");
if ($pos==false) {
print("Not found based (==) test ");
} else {
print("Found based (==) test ");
}
if ($pos===false) {
print("Not found based (===) test ");
} else {
print("Found based (===) test ");
}
?>
This script will print:
Not found based (==) test
Found based (===) test
Of course, (===) test is correct.
Submitted by:
Read Online PHP Developer Job Interview Questions And Answers
Top PHP Developer Questions
☺ | Described PHP? |
☺ | What is PEAR in PHP? |
☺ | How you can protect special characters in Query String? |
☺ | Described session in PHP? |
☺ | Described the functionality of the function strstr and stristr? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |