The latest summary of conceptual questions for PHP interview questions

藏色散人
Release: 2023-04-09 20:54:01
forward
5208 people have browsed it

This article shares with you the latest summary of conceptual questions for PHP interview questions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Related recommendations: "The latest summary of PHP interview questions and application questions"

1. HTTP status The meaning of the medium status code

  • 302: The temporary transfer was successful and the requested content has been transferred to the new location.
  • 401: Unauthorized.
  • 403: Access Forbidden.
  • 500: Server internal error

2. Passing by value and passing by reference

  • Passing by value: the actual parameters are passed Values ​​are assigned to formal parameters, and modifications to the formal parameters will not affect the values ​​of the actual parameters.
  • Pass by reference: Pass the address of the actual parameter to the formal parameter. The actual parameter and the formal parameter point to the same storage space. Modifications to the row parameters will affect the value of the actual parameter.

3. Design Pattern

Creation type: Employees act as raw materials (prototype, factory, singleton, generator, abstract factory)

Structural type: is to knock out the assembly (adapter, bridge, flyweight, appearance, agent, combination, decoration)

Behavioral type: be ordered to install the model in the dish to prevent observation (memo, chain of responsibility , command, iterator, mediator, state, template method, visitor, observer, strategy)

4. Code management

Usually a project is composed of a The team develops, and everyone submits the code they have written to the version server, and the project leader manages it according to the version, which facilitates version control, improves development efficiency, and ensures that the old version can be returned when needed.

5. The code will be executed to maliciously attack users.

How to prevent?

Answer: Use the htmlspecialchars() function to filter the submitted content and materialize the special symbols in the string.

6. CGI, FastCGI, PHP-FPM relationship diagramIn the entire website architecture, the Web Server (such as Apache) is only the distributor of content. For example, if the client requests index.html, then the Web Server will find this file in the file system and send it to the browser. What is distributed here is static data.

The latest summary of conceptual questions for PHP interview questionsIf the request is index.php, after the Web Server receives this request, it will start the corresponding CGI program, here is the PHP parser. Next, the PHP parser will parse the php.ini file, initialize the execution environment, process the request, return the processed result in the format specified by CGI, exit the process, and the Web server will return the result to the browser. This is a complete Dynamic PHP web access process.

The latest summary of conceptual questions for PHP interview questions

    Web Server:
  • Generally refers to servers such as Apache, Nginx, IIS, Lighttpd, Tomcat, etc.
  • Web Application:
  • Generally refers to PHP, Java, Asp.net and other applications.
  • CGI:
  • is a protocol for data exchange between Web Server and Web Application.
  • FastCGI:
  • Same as CGI, it is a communication protocol, but it has some optimizations in efficiency than CGI. Likewise, the SCGI protocol is similar to FastCGI.
  • PHP-CGI:
  • is the interface program of PHP (Web Application) to the CGI protocol provided by Web Server.
  • PHP-FPM:
  • is an interface program for the FastCGI protocol provided by PHP (Web Application) to the Web Server. It also provides relatively intelligent task management.

7. MVCMVC is a development model, which is mainly divided into three parts:

  • m(model), which is the model, is responsible for the operation of data;
  • v(view), which is the view, is responsible for the display of the front desk;
  • c(controller) , that is, the controller, responsible for business logic

8. PHP’s garbage collection mechanism

PHP can automatically manage memory and clear objects that are no longer needed. . PHP uses a reference counting garbage collection mechanism. Each object contains a reference counter. When a reference is connected to the object, the counter is incremented by 1. When reference leaves the living space or is set to NULL, the counter is decremented by 1. When an object's reference counter reaches zero, PHP releases the memory space it occupies.

9. Life cycle of CLI pattern


Phases Calling function Function
Module initialization phase php_module_startup() Mainly performs PHP framework, Initialization operation of zend engine
Request initialization phase php_request_startup() For fpm, it is read in the worker process and parsed A stage after requesting data
Script execution stage php_execute_script() Parse PHP syntax and generate abstract syntax tree
Request shutdown phase php_request_shutdown() Executed when the request ends
Module shutdown phase php_module_shutdown () Executed when the process is closed

10. php-fpm operating mechanism

FastCGI is a communication protocol between the web server (such as Nginx, Apache) and the processing program (such as PHP). It is a An application layer communication protocol. php-fpm is a blocking single-threaded model process manager in PHP FastCGI operating mode. It has a single master and multi-worker structure. The same worker process can only handle one request at a time. After PHP processes the request, it forwards the parsed result to the Web server through the FastCGI protocol, and the Web server returns it to the user.

Basic implementation

PHP-FPM is the implementation of fast-cgi, providing process management functions, including master and worker processes:

  • Master creates and monitors sockets, forks multiple worker processes, obtains worker status through shared memory, and then controls worker processes through signals
  • worker freely accepts requests

worker—Request processing

The worker process continuously accepts requests. When a request arrives, it will read and parse the data of the FastCGI protocol. After the parsing is completed, the PHP script will be executed, and the request will be closed after the execution is completed. The steps for each worker to process requests are as follows:

  1. Waiting for requests: The worker process is blocked in fcgi_accept_request() waiting for requests.
  2. Parse the request: After the fastcgi request arrives, it is received by the worker, and then starts to receive and parse the request data until the request data is completely arrived.
  3. Request initialization: execute php_request_startup().
  4. Execute PHP script.
  5. Close the request.

There is a parameter in the structure of the worker process to record the stage fpm_scoreboard_proc_s->request_stage the worker is currently in. During a request, this value will be set to the following values:

  • FPM_REQUEST_ACCEPTING: Waiting for the request phase.
  • FPM_REQUEST_READING_HEADERS: Read fastcgi request header phase.
  • FPM_REQUEST_INFO: Obtain request information phase. This phase saves the requested method, query string, request uri and other information into the fpm_scoreboard_proc_s structure of each worker process. This operation requires locking because the master process will also operate it. this structure.
  • FPM_REQUEST_EXECUTING: Execute PHP script phase.
  • FPM_REQUEST_END: ​​Not used.
  • FPM_REQUEST_FINISHED: Request processing completed.

master–Process Management

master will no longer return after calling fpm_run(), but will enter an event loop. From then on, master will always revolve around Several events are processed. Before analyzing these events in detail, we first introduce the three different process management methods of Fpm. The specific mode to be used can be specified through pm in the conf configuration, such as pm=dynamic.

  • Static mode (static): This method is relatively simple. At startup, the master forks out a corresponding number of worker processes according to the pm.max_children configuration, that is, the number of worker processes is fixed.
  • Dynamic mode (dynamic): This mode is more commonly used. When Fpm starts, a certain number of workers will be initialized according to the pm.start_servers configuration. During operation, if the master finds that the number of idle workers is lower than the number of pm.min_spare_servers configured (indicating that there are too many requests and the workers cannot handle them), it will fork the worker process, but the total number of workers cannot exceed pm.max_children. If the master finds that the number of idle workers exceeds pm.max_spare_servers (indicating that there are too many idle workers), it will kill some workers to avoid occupying too many resources. The master uses these four values ​​to dynamically control the number of workers.
  • Ondemand mode (ondemand): This mode is very similar to traditional cgi. It does not allocate worker processes at startup. After there is a request, it notifies the master process to fork the worker process, that is, after the request comes Then fork the child process for processing. The total number of workers does not exceed pm.max_children. The worker process will not exit immediately after the processing is completed. It will exit when the idle time exceeds pm.process_idle_timeout.

The master process enters the fpm_event_loop() event loop. In this method, the master will cyclically process several IO and timer events registered by the master. When an event is triggered, the specific handler will be called back for processing.

11. Memory allocation process

Pre-apply for a piece of memory and manage it internally in PHP. When the application applies for memory, it will apply from this part and release it first. Back to memory management. This design can avoid the additional performance consumption of the operating system caused by the application and release of small memory.

12. Implementation of PHP array

The underlying implementation of PHP array is a hash table (also called hashTable). The hash table directly accesses memory storage based on the key (Key) There is a mapping function between the key and the value of the location data structure. The hash value obtained by the mapping function can be directly indexed to the corresponding value according to the key without comparing the keyword. In an ideal situation, the hash value is not considered. Column conflicts, the hash table search efficiency is very high, the time complexity is O (1).

13. Dependency injection

Concept: refers to the way that other services that the service depends on are not created by the service itself, but are passed in from the outside.

How to achieve it? Answer: Generally speaking, it is implemented using reflection.

What problems can be solved? Answer: Reduce the coupling between service modules. When writing code, you do not need to consider the specific implementation of external services. You only need to use the service based on the interface.

14. Object-oriented

Concept: Object-oriented is a design method for programs, which helps improve the reusability of programs and makes the program structure clearer.

Main features: encapsulation, inheritance, polymorphism.

Five basic principles: Single responsibility principle; Open and closed principle; Replacement principle; Dependency principle; Interface separation principle.


This article first appeared on the LearnKu.com website.

Related recommendations: "2021 PHP Interview Questions Summary (Collection)"

The above is the detailed content of The latest summary of conceptual questions for PHP interview questions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template