if(strpos("http://www.example.com","http://www.")==0){ // do work}
I’d expect this to resolve as true, which it does. But what happens when I do
if(strpos("abcdefghijklmnop","http://www.")==0){// do work}
This also passes on php 5 because as far as I can work out the strpos returns false which translates as 0.
Is this correct thinking/behaviour? If so what is the workaround for testing for that a substring is at the beginning of another string?
Yes, this is correct / expected behavior :
strposcan return0when there is a match at the beginning of the stringfalsewhen there is no matchThe thing is you should not use
==to compare0andfalse; you should use===, like this :Or :
For more informations, see Comparison Operators :
$a == $bwill beTRUEif$ais equal to$b.$a === $bwill beTRUEif$ais equal to$b, and they are of the same type.And, quoting the manual page of
strpos: