I was looking through php-src/Zend/zend_API.c
and couldn't find the source code for the strlen()
function in PHP anywhere. Grepping through the code base didn't really help as it's littered with libc strlen
everywhere. Googling doesn't much help either.
I tried using the Vulcan Logic Dumper extension to inspect what's going on under the hood.
I tried the following code as a test:
<?php
strlen("foo"); strpos("foo", "f");
This is what I got:
Finding entry points Branch analysis from position: 0 1 jumps found. (Code = 62) Position 1 = -2 filename: /tmp/test.php function name: (null) number of ops: 7 compiled vars: none line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 3 0 E > ECHO 3 4 1 INIT_FCALL 'strpos' 2 SEND_VAL 'foo' 3 SEND_VAL 'o' 4 DO_ICALL $0 5 ECHO $0 6 > RETURN 1
Notice how strpos()
shows up as a function call, but not strlen()
. So I tried this (this on PHP 7.4, by the way) as an experiment and got something interesting.
Code
<?php
$str = "foo";
echo strlen($str);
echo strpos($str, "o");
Output from Vulcan
Finding entry points Branch analysis from position: 0 1 jumps found. (Code = 62) Position 1 = -2 filename: /tmp/test2.php function name: (null) number of ops: 9 compiled vars: !0 = $str line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 2 0 E > ASSIGN !0, 'foo' 4 1 STRLEN ~2 !0 2 ECHO ~2 5 3 INIT_FCALL 'strpos' 4 SEND_VAR !0 5 SEND_VAL 'o' 6 DO_ICALL $3 7 ECHO $3 8 > RETURN 1
Notice how all of a sudden STRLEN
shows up in the op list, but strangely enough strpos()
shows up as INIT_FCALL
. Seems like something's different about strlen()
than other functions. I tried looking through the manual to better understand how the opcodes work, but hit a dead end as not much useful information is there.
Can anyone explain why strlen()
seems to behave so different than other functions and perhaps point me to the source code for it? Perhaps the reason I cannot seem to find its source might have something to do with why it's so special? I'm not sure.