php - Why is this call to strstr not returning false? -


i've stumbled upon issue strstr in old legacy codebase. there's lot of code, test case come down this:

$value = 2660; $link = 'affiliateid=1449&zoneid=6011&placement_id=11736&publisher_id=1449&period_preset=yesterday&period_start=2017-03-27&period_end=2017-03-27';  var_dump(strstr($link, $value)); 

i expect return false since "2660" not in string returns d=1449&zoneid=6011&placement_id=11736&publisher_id=1449&period_preset=yesterday&period_start=2017-03-27&period_end=2017-03-27.

i realise $value should string still don't understand why it's not casted string php , why it's finding number in link.

actually, if try $value = '2660'; returns false expected.

any idea what's happening?

short answer

when run strstr($str, 2660) $needle resolved character "d" calling chr(2660) , therefore stops @ first "d" found in given $str, in case right @ 11th character.


why calling chr(2660)?

because when $needle not string strstr casts argument integer , uses corresponding character position extended ascii code chr(2660) "d".

if needle not string, converted integer , applied ordinal value of character.


but why chr(2660) return "d" when "d" ord(100)?

because values outside valid range [0-255] bitwise and'ed 255, equivalent following algorithm[source]

while ($ascii < 0) {     $ascii += 256; } $ascii %= 256; 

so, 2660 becomes 100, , when passed strstr it's used ordinal value of character , looks character "d".

confusing? yes. expected casted string, that's assuming things in programming. this, @ least, documented; you'd surprised amount of times weird happens , there's no official explanation found. @ least not following same link provided.


why named strstr?

i did little bit of research , found this gem (rationale american national standard information systems - programming language - c) way 1989 named functions relating strings prefix str logical, since php's source written in c explain why has carried. best guess searching string inside string, say:

the strstr function invention of committee. included hook efficient substring algorithms, or built-in substring instructions.

useful docs



Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -