This article mainly introduces the execution process of the interpreter for PHP kernel exploration. For more information, see cli (Command Line Interface), which is the Command Line mode of PHP, now this SAPI is installed by default. After installing PHP on the server, an executable file is generated. Assume that this file is/usr/local/bin/php, in SHELL, run the following command to execute a PHP script:
The Code is as follows:
/Usr/local/bin/php-f test. php
Take cli sapi as an example to parse the core part of php Execution. CLI is the php Command Line mode. This SAPI is installed by default. After PHP is installed on the server side, an executable file is generated. You can call the PHP Command in shell to execute it.
The Code is as follows:
PHP-f XX. php
Execution Process:
Parse command line parameters;
Initialize the environment;
Compile and execute PHP code;
Clean up the environment and exit;
How to execute the PHP script in the 3rd phase:
Call php_execute_script (handle_file) to complete the third stage. This function will eventually call zend_execute_scipts (... ), This function is a variable parameter function that can execute multiple PHP scripts at a time.
In zend_execut_scripts (.....) The core function calls the (zend_compile_file) (compile_file) and (* zend_execute) (zend_op_array) functions;
Call zend_compile_file to compile the php script file specified by the parameter. This function returns a structure pointer of zend_op_array;
The input parameter zend_execute is the return value of zend_compile_file, and opcode is executed.
These two functions are Zend APIs and are a function pointer. The specific method for assigning values when the engine is initialized is called.
Ps: Why are these two Zend APIs function pointers?
During engine initialization, zend_execute and zend_compile_file point to the default method during engine initialization. We can compile and execute the rewrite function, which leaves a hook for our extension engine. For example, vld points zend_execute and zend_compile_file to the function encapsulated by the original function and adds the opcode output.