Home > Article > Backend Development > PHP kernel exploration of the execution process of the interpreter, _PHP tutorial
cli (Command Line Interface) is the command line mode of PHP. Now this SAPI is installed by default. We are on the server After installing PHP, an executable file will generally be generated. Assuming that this file is /usr/local/bin/php, then we can use the following command to execute a PHP script under SHELL:
Copy code The code is as follows:
/usr/local/bin/php -f test.php
Take CLI SAPI as an example to analyze 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, which can be executed by calling the PHP command in the shell.
Copy code 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;
In the third stage, how to execute the PHP script:
Complete the third stage by calling php_execute_script(handle_file), which will eventually call zend_execute_scipts(…). This function is a variable parameter function that can execute multiple PHP scripts at one time.
In the zend_execut_scripts(…..) function, the core calls the two functions (zend_compile_file)( compile_file ), (*zend_execute)( zend_op_array );
By calling zend_compile_file to compile the php script file specified by the parameter, this function will return a zend_op_array structure pointer;
The parameter passed in to zend_execute is the return value of zend_compile_file, and the opcode starts to be executed.
These two functions are Zend API, which is a function pointer that returns specific methods when the engine is initialized.
ps.: So why are these two Zend APIs function pointers?
When the engine is initialized, zend_execute and zend_compile_file will point to the default methods when the engine is initialized. We can override function pointers during compilation and execution, leaving hooks for us to extend the engine. For example: vld points zend_execute and zend_compile_file to its own function that encapsulates the original function, and adds the output of opcode information.