How to understand the principle of PHP operation, specific look at the code?

Source: Internet
Author: User
Keywords Php
Tags php server
   PHP 测试   
  
   Hello World

This is a simple PHP code, the file is hello.php, I request this page through www.xxx.com/hello.php, PHP is how to parse the data back to the server, returned to me, the process is how. My understanding is that when I access this file, the server found the PHP script, and then called the PHP script parser to parse the PHP code inside, then echo means to return the data directly, the return data can only use Echo, The server consolidates the data returned by HTML and PHP and sends it to the client, which I directly access, and what to do if the page relies on other global variables. Other languages have an initialized object that initializes the entire application, and then PHP runs a specified script directly, without initializing the word.

Reply content:

   PHP 测试   
  
   Hello World

This is a simple PHP code, the file is hello.php, I request this page through www.xxx.com/hello.php, PHP is how to parse the data back to the server, returned to me, the process is how. My understanding is that when I access this file, the server found the PHP script, and then called the PHP script parser to parse the PHP code inside, then echo means to return the data directly, the return data can only use Echo, The server consolidates the data returned by HTML and PHP and sends it to the client, which I directly access, and what to do if the page relies on other global variables. Other languages have an initialized object that initializes the entire application, and then PHP runs a specified script directly, without initializing the word.

Let's just leave the web and run the command directly in your hello.php folder.php hello.php
You will see the output below (1):

   PHP 测试   

Hello World

This is the result of the PHP interpreter executing your hello.php, yes, there is no half-dime relationship with the web now.
What does the PHP program do to hello.php:

    • 1. Open hello.php file;

    • 2, from top to bottom scanning hello.php file content;

    • 3, if found in accordance with PHP syntax to execute code, the general logic code executes directly, but such as code inside the output statement (such as Echo print_r, etc.), the result output;

    • 4, if the string is not inside, then the PHP program does not know these strings, can not be processed, directly output them on the line.

The PHP interpreter can do just a few things, and the output you see above is also in this process.

So, for now, it's PHP and the web.
Your browser opens http://www.xx.com/hello.php, this request is your Web service program (Nginx, Apache ...). Received, it (Web service program) according to your configuration know that the request should be to the PHP interpreter to execute (and like CSS, JS, pictures and other static files are not explained to the PHP interpreter, the Web service program directly processing is OK), it is based on your configuration to get the root directory of the site there , you know that the request should correspond to your hello.php file, then it sets the relevant request information (HTTP request first) into the environment variable, and then notifies the PHP interpreter to explain the hello.php,php interpreter as it was initially explained Hello.php, and output (1), the Web The service program then returns the output of the PHP interpreter to the browser (with some additional information, such as an HTTP header, of course).

Simply put, the Web service program tells PHP to execute that file and then returns the results of the PHP execution to the browser. This is a process in general, but there are some differences between the configuration of CGI, fastcgi, PHP-FPM, and the interaction between the Web service program and the PHP interpreter.
This whole process is similar on any web, especially the language of the scripting class, without changing the interpreter or the way the Web service program and the interpreter communicate.

I hope you do not go into the misunderstanding: in fact, there is no need to understand this, like not know the cooling principle of air conditioning does not affect the use of the same. If you want to develop a language, but if you have the ability to develop a language, you certainly will not ask this question. Why did you go into the wrong place?

And the example you cite is not very related to PHP.
1-run PHP files through xxx.com/hello.php, this is what your Web server does, and it is not related to PHP.
2-is the Web server receiving your request, then locate the hello.php file and then call the PHP interpreter to parse the file
3-php parsing hello.php Returns parsing results to the Web server
4-web server returns results to browser
The browser parses the result into a Web page that appears in front of you.
This is the whole process, and PHP only takes effect in step 3.

and human-computer interaction is a very stupid thing, in the non-graphical interface, the user always input characters, the interpreter is interpreted according to the predetermined rules, and then return the character.

So, you cite the core of the example, when PHP explains what hello.php did? To determine whether the PHP code is not returned as is, executes and returns the execution result. How to judge is not PHP code it? It's written in , otherwise it's not.

So, the basics are important. First understand the basic concepts of Web requests, HTTP protocols, server-side scripts, browser-side scripts, and so on, your problem spans are too large.

PHP doesn't just explain Hello world.

';?> it explains the entire page,

You can try to search the CGI

When the user code input to the PHP kernel to execute, the PHP kernel will be the PHP code lexical analysis and parsing, lexical analysis is to separate the PHP code into a "unit" (TOKEN), the parsing of these "units" into the Zend engine executable operation. The Zend engine inside PHP then executes the operations sequentially. The Zend engine is a core part of the PHP kernel, responsible for the execution of the final operation and the return of the results, which can be understood as the "engine" in the PHP kernel.

phpNot only to parse the content inside, it is to explain the entire page, when you request the server, the server returned to your code (that is, the html Web page source code) is already parsed on the service side of the code, and then to the browser to render the operation, etc. But now the way to compare fire is to splice html these to the browser side to do.

This direct view of the PHP source code is the clearest

So, you cite the core of the example, when PHP explains what hello.php did? To determine whether the PHP code is not returned as is, executes and returns the execution result. How to judge is not PHP code it? It's written in, otherwise it's not.

Thank you very much for your answer, for the above paragraph would like to continue to consult, if the hello.php contains both HTML and PHP server to see the PHP suffix will be sent to the PHP parser processing, rather than the first to resolve the PHP parser to send to PHP. You said. Return the execution result, echo this output represents one kind of execution result, other such as arithmetic, if do not apply echo is not to return, temporarily so much also please enlighten

The landlord asked the main thing is: Other languages have an initialization of things, the initialization of the entire application, then PHP directly run a specified script, do not initialize this said.

This sentence?
What do you mean by initialization, not "compiling"???

And PHP is the scripting language, it is executed when the "compile", rather than compile first, then execute.
Of course, there are many scripting languages, not just PHP.

Lou Zhu, what do you mean by this?

This involves the knowledge of CGI.

phpIt is clear that the entire process of requesting a file from the user to explain php the execution of the script file to the server and returning the result to the user @incNick is explained. But the landlord doubts the place is the php interpreter how to achieve the "initialization" process, which is actually the work of the CGI principle.

Wikipedia says:

The way CGI works, from a Web server perspective, is defined in a specific location (for example: http://www.example.com/wiki.cgi) that can run CGI programs. When a request for a matching URL is received, the corresponding program is called and the data sent by the client is entered as input. The output of the program is collected by the Web server, with the appropriate file headers, and then sent back to the client.

In the php script example, when the Web server encounters a matching php end-of-URI request, the interpreter is started php and the relevant data sent by the client (for example, the URI of the user request, the corresponding get and post data, etc.) is passed to the php interpreter through the environment variables. The php interpreter then sets the global variables such as these in the context of parsing the script to be used by the $_GET, $_POST, $_SERVER script, then parses the entire script content, the HTML is output, the PHP code executes the output, the final HTML page is passed back to the Web server, and the Web server is returned to the user To complete the entire response process.

Therefore, the initialization process is to set these global variables according to the environment variables set by the server, no other. and the Web server sets which environment variables are at the center of the entire CGI protocol.

  • Related Article

    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.