phpechoimplementationphp-internalslanguage-construct

Where is PHP echo implemented in the source?


You can lookup built-in functions by searching for e.g. PHPAPI(stream_copy_to_stream) and find the implementation in ext/standard/streamsfuncs.c.

How to do that for a language construct like echo?

I found it is associated with T_ECHO in Zend/zend_language_parser.y but couldn't trace how it works from there.

Where does Zend Engine implement echo functionality?


Solution

  • Given this example program:

    <?php echo 'Hello world!';
    

    We can obtain the opcodes that the program is compiled to like so:

    $ phpdbg7.4 -p /tmp/hello.php
    function name: (null)
    L1-2 {main}() /tmp/hello.php - 0x7fef35ea1000 + 3 ops
     L1    #0     EXT_STMT                                                                              
     L1    #1     ECHO                    "Hello world!"                                                
     L2    #2     RETURN<-1>              1
    

    As you can see, echo is handled using a separate VM instruction: ZEND_ECHO. The handlers for each opcode is defined in Zend/zend_vm_def.h. Search for "ZEND_ECHO".