How does php work? Depends on the code?

Source: Internet
Author: User
{Code ...} this is a simple php code. The file is hello. php, I use www. xxx. comhello. when php requests this page, how does php parse the data and return it to the server? What is the process like. My understanding is that when I access this file, I Will server it...
   PHP Testing   
  Hello World

';?>

This is a simple php code. The file is hello. php. My understanding is that when I access this file, the server finds this php script and calls the php script parser to parse the php code in it, then, Does echo mean that data is directly returned, and only echo can be used to return data? The server integrates the data returned by html and php and sends it to the client. This page is accessed directly by me, what should I do if this page depends on other global variables. There is an initialization in other languages. If the entire application is initialized, php will directly run a specified script without initialization.

Reply content:
   PHP Testing   
  Hello World

';?>

This is a simple php code. The file is hello. php. My understanding is that when I access this file, the server finds this php script and calls the php script parser to parse the php code in it, then, Does echo mean that data is directly returned, and only echo can be used to return data? The server integrates the data returned by html and php and sends it to the client. This page is accessed directly by me, what should I do if this page depends on other global variables. There is an initialization in other languages. If the entire application is initialized, php will directly run a specified script without initialization.

In this case, you should leave the web alone and directly run the command in your hello. php folder.php hello.php
The following output is displayed: (1 ):

   PHP Testing   

Hello World

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

  • 1. Open the hello. php file;

  • 2. Scan the hello. php file from top to bottom;

  • 3. If The code is executed according to the php syntax. The general logic code is executed directly. However, if the code contains an output Statement (such as echo print_r), the result is output;

  • 4. If the string is not in The php program does not recognize these strings and cannot process them. Simply output them.

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

Now, php and web are used.
Open http://www.xx.com/hello.php in your browser. This request is sent to your Web Service Program (nginx, apache ......) Received, it (web Service Program) according to your configuration, we know that this request should be executed by the php interpreter. (For static files such as css, js, and images, you do not need to explain them to the php interpreter, the web service program can process it directly), and then it obtains the root directory of the Website Based on your configuration, and it will know which hello the request corresponds. php file, and then it sets the relevant request information (HTTP Request Header) to the environment variable, and then notifies the php interpreter to explain hello. php and php interpreters interpret hello as they did at the beginning. php, and output (1). the web service program then returns the php interpreter output to the browser (of course, some additional information, such as the HTTP header ).

Simply put, the web Service Program tells php to execute the file and then returns the php Execution result to the browser. As a whole, the configuration of cgi, fastcgi, and php-fpm differs in the interaction between the web Service Program and the php interpreter.
This entire process is similar on any web, especially in the script language. It almost changes to an interpreter or communication mode between the web service program and interpreter.

I hope you will not go into the misunderstanding: in fact, you do not need to understand this, just as you do not know the refrigeration principle of the air conditioner does not affect the use. If you want to develop a language, but if you have the ability to develop a language, you will not ask this question. Why are you mistaken?

In addition, the example you mentioned has little to do with PHP.
1-run the PHP file through xxx.com/hello.php. This is what your web server is doing and is irrelevant to PhP.
2-The web server receives your request, finds the file hello. php, and calls the php interpreter to parse the file.
3-php parses hello. php and returns the resolution result to the web server.
4-The web server returns the result to the browser
5-The browser parses the result into a webpage to present it to you.
This is the entire process, while php only works in step 3.

Human-Computer Interaction is a very dumb. on the premise of a non-graphic interface, the user always enters characters, the interpreter interprets the characters according to the predefined rules, and then returns the characters.

So, the core part of your example, when php explains what hello. php has done? To determine whether php code is used, if it is not returned as is, it is executed and the execution result is returned. How can we determine if it is a php code? Written in That is, otherwise none.

Therefore, the foundation is very important. First, understand the basic concepts of web requests, http protocols, server scripts, and browser scripts. Your problem is too long.

PHP not only explains Hello World

';?> It explains the entire page,

You can try to search for cgi

When user code is input to the PHP kernel for execution, the PHP Kernel performs lexical analysis and syntax analysis on the PHP code, lexical analysis divides PHP code into tokens. syntax analysis converts these tokens into executable operations of Zend Engine. Then the Zend Engine in PHP executes these operations sequentially. Zend Engine is the core part of the PHP kernel. It is responsible for the execution of final operations and the return of results. It can be understood as the "Engine" in the PHP kernel ".

phpIt's not just about Parsing The content in. It explains the entire page. When you request the server, the server returnshtmlCode (that is, the webpage Source Code) is the parsed code on the server side, and then the rendering and other operations on the browser side, but now it is quite popular to splicehtmlThese are done on the browser side.

The php source code is the clearest.

So, the core part of your example, when php explains what hello. php has done? To determine whether php code is used, if it is not returned as is, it is executed and the execution result is returned. How can we determine if it is a php code? If not.

Thank you very much for your answer. I would like to continue to ask you for the above paragraph, if hello. php contains both html and php servers and sends php suffixes to the php parser for processing, instead of parsing php before sending it to the php parser. What you said is to return the execution result. echo indicates an execution result, and other operations such as operations. If echo is not applicable, it will not be returned. Please enlighten me if so many operations are available.

The main question raised by the author seems to be: Does php directly run a specified script when initializing the entire application in other languages.

This sentence?
Does initialization mean "Compilation "???

Php, however, is a scripting language. It is "compiled" after execution, rather than "compiled" before execution.
Of course, there are many scripting languages, not just php.

What do you mean by that?

This involves cgi knowledge.

RequestphpFilephpThe interpreter interprets and executes the script file and then returns the explain result to the user on the server.@incNickThe explanation is clear. What the landlord is confused about is:phpHow the interpreter implements the "initialization" process is actually the working principle of CGI.

Wikipedia says:

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

TophpFor example, when the web server encounters a matchingphpAt the end of the uri requestphpInterpreter, and pass the data sent by the client (for example, the user request uri, the corresponding get and post data) tophpInterpreter, and thenphpBased on these environment variables, the interpreter Sets$_GET, $_POST, $_SERVERAnd other global variables for the script to use, and then parse the entire script content, html output as is, php code execution output, get the final html page, passed back to the web server, the web server returns the response to the user to complete the response process.

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

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.