Some basic concepts of PHP are combed

Source: Internet
Author: User
Tags apc hhvm sapi zend

Lou Zhu is a novice who just joined the work, these days just opened the blog Park Weibo, want to through this platform with you to learn and share some technical knowledge.

But landlord hesitated for a long time, do not know the first article should write something. Finally, I decided to start with some basic PHP concepts in order to deepen my initial understanding of each concept.

For this motive, I read some information and did a summary of the study, thus having this maiden blog.

1php is a compiled or interpreted type

This seems to be a simple question, but it is not a simple matter to answer clearly. It's important to figure out what the compiler is and what the explanatory type is.

Translation

Computers can not directly understand the high-level language, only directly understand the machine language, so the high-level language must be translated into machine language, the computer to perform high-level language programming programs. There are two ways of translating, one is compiling, the other is explaining.

Compile

The type language writes the program to execute, needs a specialized compiles the procedure, compiles the program to be the machine language file, for example the EXE file, later must run the words to need not to re-translate, the direct use compiles the result to be OK (exe file)

Explain

Interpretive language programs do not need to compile, save the process, the interpretation of the language in the process of running the translation, such as the explanatory Basic language, a special interpreter can directly execute the BASIC program, each statement is executed when the translation. Such explanatory language should be translated once every time it is executed.

Classification

Java is compiled by the compiler into Java bytecode (Java bytecode), and then at run time through the interpreter to interpret bytecode into machine code;

C # is a compiler that compiles C # code into Microsoft Intermediate Language (Microsoft Intermediate Code) and then compiles IL into machine code through the CLR;

PHP (after 4.0) is compiled by Zend Engine (Ze) into the OP Code intermediate code, and then during the execution phase Ze interpret and execute the OP code generated during the compilation phase.

So, strictly speaking, Java and PHP are a compiled and interpreted language, and C # is a purely compiled language and needs to be compiled two times.

To say two more words, why C # to compile two times, we all know that C # is managed code for. Net. NET managed code such as C #, VB. NET code, is compiled into the intermediate language (Il,intermediate Language, at run time, and then by the instant compiler (JIT,JUST-IN-TIME) compile the cost machine code.

Jit

JIT is just in time, instant compilation technology. Using this technique, you can speed up the execution of a two-time translation program. JIT will save the translated machine code, has been prepared for the next use, so theoretically, the use of the JIT technology can be close to the previous pure compilation technology.

How does C # and Java support jit,php's JIT support at this time? HHVM is the Facebook-developed high-performance Facebook release of the new JIT PHP virtual machine, claiming 9 times times faster than the official. The landlord happened to see, did not do in-depth understanding http://www.oschina.net/news/50112/how-hhvm-improve-php-performance?p=2

JIT schematic diagram of the work (Java)

The Java JIT compiler, the industry first developed a JIT (just in time) compiler. When Java executes the runtime environment, each time a new class is encountered, the class is a functional group in a Java program-jit compiler will compile (compile) jobs for this class at this time. The compiled program is optimized to be a fairly streamlined binary, and this program executes fairly quickly. It takes a little time to compile to save a considerable amount of execution time later, and the JIT design does add a lot of efficiency, but it does not achieve the best performance, because some of the very few Java instructions that are executed at compile time may be more time-consuming than when the translator is executing, for these instructions, The overall time spent did not decrease.

Based on the experience of JIT, the industry has developed dynamic compilers (compiler), which are compiled only for more commonly executed code, while the remainder is still executed using a translator. In other words, the dynamic compiler will determine whether to compile each class. The dynamic compiler has two tools: one is the translator and the other is the JIT, which analyzes each class through the intelligence mechanism and then decides which of these two weapons to use to achieve optimal results.

Apc

Alternative PHP cache (APC) is an open and free php opcode cache. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP's intermediate code.

apc.stat=1/0

Whether this option enables script update checking. Be very careful to change this command value. The default value on indicates that APC checks that the script is updated every time the script is requested, and automatically recompile and cache the compiled content if it is updated. But doing so has a detrimental effect on performance. If set to Off, no check is made, resulting in a significant performance gain. However, in order for the updated content to take effect, you must restart the Web server (translator Note: If you use cgi/fcgi similar, you need to restart the cgi/fcgi process). The script files on the production server are rarely changed and can achieve significant performance gains by disabling this option.

This command is also valid for Include/require files. However, it should be noted that if you are using a relative path, APC must check each time Include/require to locate the file. Using an absolute path allows you to skip the check, so you are encouraged to use an absolute path for include/require operations.

2PHP Operation Mechanism Analysis process

1, the above example, passed to the PHP program needs to execute the file, the PHP program to complete basic preparation work after the launch of PHP and Zend engine, load the registered extension module.

2. After initialization, read the script file, zend The script file for lexical analysis and parsing. It is then compiled into opcode execution. If an opcode cache such as APC is installed, the compile link may be skipped and read directly from the cache opcode executed

Sapi

SAPI is defined as a programming interface for specific PHP applications, but it is definitely not a specification for PHP code, but a running canonical interface throughout the PHP code life cycle. PHP five operating modes: including CGI, fast-cgi, CLI, ISAPI, Apache module DLL, respectively, is the implementation of the SAPI interface.

php-fpm

The full name is the PHP fastcgi process Manager, the PHP fastcgi processes manager, which dynamically evokes the CGI process and destroys it to reach dynamic tuning CGI numbers compared to the fastcgi static arousal CGI,FPM, based on the pressure of the access. This allows for efficient use of memory. In addition, there are other advantages, such as FPM can also be smooth overloaded PHP configuration, because FPM uses Unix-socket to communicate with the server, so there is no need to configure the CGI port; fpm has better status output and Slowlog logs, You can give more error details when you are 502.

The directives are as follows:

/USR/LOCAL/PHP/SBIN/PHP-FPM {start|stop|quit|restart|reload|logrotate}

Zend Engine

Zend is implemented as a whole with pure C, which is the kernel part of PHP, Java-like JVM.

It will be the PHP code translation (lexical, grammatical parsing, etc.) for the execution of opcode processing and implementation of the corresponding processing methods, the implementation of basic data structures (such as Hashtable, OO), memory allocation and management, providing the corresponding API method for external calls, is the core of all, All peripheral functions are implemented around the Zend.

Extensions

Around the Zend Engine, extensions provides a variety of basic services through a component-based approach, our common set of built-in functions (such as the array series), standard libraries, etc. are implemented through extension, and users can implement their own extension as needed to achieve functional expansion , performance optimization and other purposes (such as the PHP middle layer is in use, Rich text parsing is the typical application of extension).

Five PHP operating modes included

Http://www.phpernote.com/news/723.html

Cgi

The way in which a connection request is encountered (user request) is to create a child process of the CGI, activate a CGI process, then process the request, and end the child process after processing. This is the Fork-and-execute mode. So how many CGI sub-processes are there for the CGI server, and the sub-process loading is the main reason for the poor CGI performance.

fast-cgi

is an upgraded version of CGI, fastcgi is like a resident (long-live) CGI, which can be executed all the time, so long as it is activated, it does not have to take a moment to fork it every time. PHP uses PHP-FPM (FastCGI process Manager), the full name of the PHP FastCGI process Manager to manage.

1. Load the FASTCGI Process Manager (IIS ISAPI or Apache Module) when Web server starts

2, the FASTCGI process manager itself initializes, starts multiple CGI interpreter processes (visible multiple php-cgi) and waits for a connection from the Web server.

3. When a client request arrives at Web server, the FASTCGI process manager selects and connects to a CGI interpreter. WEB server sends CGI environment variables and standard input to the FASTCGI child process php-cgi.

4. After the fastcgi process finishes processing, the standard output and error information are returned from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed to completion. The fastcgi child process then waits and processes the next connection from the FASTCGI process Manager (running in Web server). In CGI mode, php-cgi exits here.

In the above scenario, you can imagine how slow CGI is usually. Every Web request PHP must re-parse php.ini, reload all extensions, and initialize all data structures. With fastcgi, all of this occurs only once when the process is started. An additional benefit is that the persistent database connection (persistent connection) can work.

Cli

PHP is the command-line mode of operation, we often use it, but may not notice (for example: we often use "php-m" under Linux to find PHP installed those extensions is the PHP command line running mode, interested students can input php-h to delve into the operation mode)

Isapi

That is, the Internet Server application program Interface, Microsoft provides a set of Internet service-oriented API interface, an ISAPI DLL, can be activated after the user request to long-standing memory, waiting for another user request, You can also set up multiple user request handlers in a DLL, and the ISAPI DLL application and the WWW server are in the same process and are significantly more efficient than CGI. (due to the exclusivity of Microsoft, only running in the Windows environment)

The DLL run mode of Apache module

This mode of operation is often used by Apache servers in a Windows environment, and in a modular (DLL), PHP is started and run with a Web server.

Some basic concepts of PHP are combed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.