PHP: "Does the storage rack have needles?"

Assuming this works correctly to tell if there is a substring in a string, is there a more concise way to do this?

if(is_int(strpos($haystack, $needle))){
   ...
}
+3
source share
2 answers

I wouldn’t do that. Just make a rigorous comparison with FALSE.

$found = strpos($haystack, $needle) !== FALSE;
+5
source

Not really. It really depends on your preferences, for which one of them is the clearest way to express what you are doing. Some alternatives:

if( strpos( $h, $n ) !== FALSE ){
    // stuff
}

if( strpos( $h, $n ) > -1 ){
    // stuff
}

The most common approach is probably to use strict comparisons FALSE, so if you are working on an open source project or have many other people using your code, consider this option.

+3
source

All Articles