Short answer
When you run strstr($str, 2660)
the $needle
is resolved to the character "d" by calling chr(2660)
and therefore it stops at the first "d" found in the given $str
, in this case right at the 11th character.
Why are we calling chr(2660)
?
Because when the $needle
is not a string strstr
casts that argument to an integer and uses the corresponding character for that position from the extended ASCII code where chr(2660)
is "d".
If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
But why does chr(2660)
return "d" when "d" is ord(100)
?
Because values outside the valid range [0-255]
will be bitwise and'ed with 255
, which is equivalent to the following algorithm[source]
while ($ascii < 0) {
$ascii += 256;
}
$ascii %= 256;
So, 2660
becomes 100
, and then when passed to strstr
it's used as the ordinal value of the character and looks for character "d".
Confusing? Yes. I also expected it to be casted to a string, but that's what we get for assuming things in programming. This, at least, is documented; you'd be surprised the amount of times something weird happens and there's no official explanation to be found. At least not as easily as following the same link you provided.
Why is it named strstr
?
I did a little bit of research and found this gem (Rationale for
American National Standard
for Information Systems -
Programming Language -
C) from all the way back 1989 where they named all the functions relating to strings with the prefix str
which is logical, then since PHP's source is written in C it will explain why it has carried. My best guess is that we are searching for a string inside another string, they do say:
The strstr function is an invention of the Committee. It is included
as a hook for efficient substring algorithms, or for built-in
substring instructions.
Useful docs
- Documentation for
strstr
- Documentation for
chr
- PHPwtf a good resource for weirdness
var_dump(strstr($link, (string)$value));
PHP is not a strict language and tends to give strange results, when different types are being compared. Just like(1=="a1")
equals as true. – Speedwellstrstr
finds a string inside another string. Don't blame PHP for this; the name comes from C. – Overeat