Where is the source code for strlen() function in PHP?

后端 未结 1 1431
逝去的感伤
逝去的感伤 2021-01-13 23:29

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 th

1条回答
  •  暖寄归人
    2021-01-14 00:04

    strlen() is actually an opcode in PHP 7 and thus doesn't behave like a typical function. Its source code is located in php-src/Zend/zend_string.h on line 53 (as of the time of this writing), which is defined as a Macro.

    #define ZSTR_LEN(zstr)  (zstr)->len
    

    It's basically just reading the len member of the _zend_string struct, which stores the length of the string as a member.

    If you look at the git-blame you'll see that particular macro was added around the time of PHP 7's release in 2015.

    Here's the commit: https://github.com/php/php-src/commit/4bd22cf1c1d6a262fe2f026e082f2565433c53df

    Here's what my git log says:

    commit 4bd22cf1c1d6a262fe2f026e082f2565433c53df
    Author: Dmitry Stogov 
    Date:   Mon Jun 29 16:44:54 2015 +0300
    
        Improved zend_string API (Francois Laupretre)
    
        Squashed commit of the following:
    
        commit d96eab8d79b75ac83d49d49ae4665f948d15a804
        Author: Francois Laupretre 
        Date:   Fri Jun 26 01:23:31 2015 +0200
    
            Use the new 'ZSTR' macros in the rest of the code.
    
            Does not change anything to the generated code (thanks to compat macros) but cleaner.
    
        commit b3526439104ac7a89a8e0c79dbebf33b22bd01b8
        Author: Francois Laupretre 
        Date:   Thu Jun 25 13:45:06 2015 +0200
    
            Improve zend_string API
    
            Add missing methods
    

    So it looks like at some point around PHP 7's release some improvements were made to the API. It's unclear if this means strlen() went from being a function to being an opcode, or if it was always an opcode.

    I do see, though, how the interned string stuff might effect the output of Vulcan in your experiment. If you use an interned string PHP seems to take some kind of shortcut in the executor for some reason. I'm unclear as to how, exactly, but yes it does appear to behave somewhat differently from a typical function.

    0 讨论(0)
提交回复
热议问题