A summary of new PHP interview questions for Baidu, Ali, Meituan and other big companies in the spring of 2021 (1)

Gold three silver four started, so it took a long time to collect and sort out this set of PHP interview questions, I hope it will be helpful to everyone~

The blogger also organized a large number of interview questions into a PHP interview manual, which is a PDF version, and there are ways to obtain them at the bottom of the article.

1. Please introduce yourself?

Answer: My name is xxx, I am from Beijing. I graduated from the Computer xx Department of xx University in 20xx. After graduation, I worked in php development for x years in **. The company is an outsourcing company, mainly doing WeChat development, public account promotion, and shopping malls. Forum development

2. Which projects are you responsible for in the company?

Answer: Since our company is an outsourcing company, it is impossible to do only one project. The company's projects are all cross-cutting. I have done all the forums, WeChat, and shopping malls.

3. Which modules have you made?

Answer: Login and registration, product management, shopping cart module, order management

4. What framework does your company use?

Answer: Our company uses the TP framework, using mysql+apache+php for development, because the TP framework is a free and open source, lightweight PHP development framework, and it is developed by our Chinese ourselves and used in China. There are more, and all kinds of information are more complete

5. What is mvc? What is the relationship between each other?

Answer: mvc is a development model, which is mainly divided into three parts: m (model), which is the model, responsible for data operation; v (view), which is the view, responsible for the display of the front and back ends; c (controller), also It is the controller, the controller responsible for the business logic client requesting the project. If data is needed during the execution process, the controller will get the data from the model, and then display the obtained data through the view.

6.What is oop?

Answer: oop is object-oriented programming. Object-oriented programming is a computer programming architecture. A basic principle of OOP is that a computer program is composed of a single unit or object that can function as a subroutine.

OOP has three characteristics

1. Encapsulation: Also known as information hiding, it separates the use and implementation of a class, and only keeps some interfaces and methods in contact with the outside, or only exposes some methods for developers to use. Therefore, developers only need to pay attention to how this class is used instead of the specific implementation process. In this way, MVC division of labor can be realized, interdependence between programs can be effectively avoided, and loose coupling between code modules can be realized.

2. Inheritance: the subclass automatically inherits the properties and methods of its parent class, and can add new properties and methods or rewrite some properties and methods. Inheritance increases the reusability of the code. PHP only supports single inheritance, which means that a child class can only have one parent class.

3. Polymorphism: The subclass inherits the properties and methods from the parent class, and rewrites some of the methods. So although multiple subclasses have the same method, the objects instantiated by these subclasses can get completely different results after calling these same methods. This technique is polymorphism. Polymorphism enhances the flexibility of the software.

7. What is smarty and what does it do?

Answer 1: Smarty is a template engine written in php, and it is also one of the most famous php template engines in the industry.

It separates the logic code and the external display, and provides an easy-to-manage and use method to separate the mixed php logic code from the html code

Answer 2: Smarty is one of the most famous engine frameworks in PHP. Our company uses the TP framework, and the smarty template has been encapsulated, so it has not been used alone.

Answer 3: Smarty is a template engine. The most notable feature is that it can cache templates. Generally speaking, a template is to make a static page, and then cut some dynamic parts in it with all separators, then open the template file in PHP, replace the value in the separator, and then output it, you You can look at the template part in PHPLib.

After smarty sets the cache parameters, the template will be opened the first time it is run. When php replaces the value inside, it will regenerate the read html and php part into a temporary php file, which saves the need to open each time. Read the html again. If you modify the template, just refresh it again.

8.What are the advantages of the TP framework?

Answer: The TP framework is a framework developed by us Chinese ourselves. Various materials are relatively complete, and they are used more domestically. It is relatively simple and convenient, and it is free and open source.

9.What are the characteristics of TP?

1. Multi-table query is very convenient, a few lines of code in the model can complete the associated operation of the multi-table

2. Incorporating smarty templates to separate the front and back ends

3. Support a variety of caching technologies, especially for memcache technical support is very good

4. Naming conventions, models, views, and controllers strictly follow the naming rules, and one-to-one correspondence through naming

5. Support multiple url modes

6. Built-in ajax return methods, including xml, json, html, etc.

7. Support application extension, class library extension, driver extension, etc.

10.The big letter function in the TP framework?

U: assembly of url

A: Internally instantiate the controller

S: Cache processing

R: Call the operation method of a certain controller

D: instantiate a custom model class

M: instantiate the base model class

I: Get parameters

L: Set or get the current language

C: Set or get, save configuration

11.Please introduce the laravel framework?

Answer: The design idea of ​​the laravel framework is relatively advanced and it is very suitable for applying various development modes. As a framework, it has everything ready for you. Composer is the future of php. Without composer, php will definitely go into decline.

The biggest feature and excellence of the laravel framework is the collection of relatively new features of PHP, as well as various design patterns, Ioc patterns, dependency injection, etc.

12.What are the characteristics of laravel?

Answer one:

1. Powerful rest router: you can call it with a simple callback function, and quickly bind the controller and router

2.artisan: command line tool, many manual tasks are automated

3. Inheritable templates to simplify view development and management

4.blade template: faster rendering

5. ORM operation database

6.migration: manage database and version control

7. The test function is also very powerful

8. Composer is also a highlight

Answer 2: The laravel framework introduces facade, dependency injection, Ioc mode, and various design patterns, etc.

13. Please briefly describe the optimization of the database?

1. From the structural layer: web server adopts load balancing server, mysql server adopts master-slave replication, read-write separation

2. From the storage layer: adopt a suitable storage engine and adopt a three-paradigm

3. From the design level: use partitioning and sub-tables, indexes, and table fields use appropriate field attributes, appropriately use inverse paradigms, and turn on mysql caching

4.SQL statement layer: In the case of the same result, use high-efficiency, fast-speed and resource-saving SQL statement execution

14. How to solve the exception handling?

Answer: Throw an exception: use try...catch, the exception code is placed in the try code block, if no exception is triggered, the code continues to execute, if the exception is triggered, an exception will be thrown. The Catch code block catches the exception and creates an object containing the exception information. $e->getMessage(), output abnormal error message.

Solve the exception: use the set_error_handler function to get the exception (you can also use the try() and catch() functions), and then use the set_exception_handler() function to set the default exception handler, and the register_shutdown_function() function to execute. The imported function is transferred to the memory, and this function is called when all the php statements on the page are executed.

15.Implementation of Rights Management (RBAC)?

1. First create a user table: id name auto (save format: controller-method)

2. Then create a base class controller in the background, and encapsulate a construction method in the controller. When the user logs in successfully, use the session function encapsulated in the TP framework to obtain the session id stored in the server, and then instantiate the model. Obtain the auth data stored in the data table through the user id, use the explode function to divide the obtained data, and save it using an array, and then use the encapsulated constants in the TP framework to obtain the current controller and method, and then assemble them into String, use the in_array function to determine whether the array contains the currently acquired controllers and methods. If not, it will prompt the user that there is no permission, and if there is, proceed to the next step.

16.How to ensure that promotional items will not be oversold?

Answer: This problem is a difficulty we encountered during development at the time. The main reason for oversold is the inconsistency between the number of orders placed and the number of products we want to promote. Each time the number of orders is always higher than our promotional products The number is much larger. At that time, our group discussed for a long time and gave several solutions to achieve:

The first solution: Before placing an order, we judge whether the quantity of the promotional products is enough, and the order is not allowed if it is not enough. When changing the inventory quantity, add a condition to only change the inventory of the goods whose inventory is greater than 0. At that time, we used ab Under stress test, when the concurrency exceeds 500 and the number of visits exceeds 2000, oversold will still occur. So we denied it.

The second solution: use mysql transaction plus exclusive lock to solve, first we choose the storage engine of the database as innoDB, which is realized by using exclusive lock. At the beginning, we tested the shared lock and found that there would still be oversold The phenomenon. One problem is that when we conduct high-concurrency tests, it has a great impact on the performance of the database, which leads to a lot of pressure on the database, which is ultimately rejected by us.

The third solution: use file locks to achieve. When a user grabs a promotional item, the file lock is triggered to prevent other users from entering. After the user grabs the promotional item, the file lock is unlocked and other users are released for operation. This can solve the problem of overselling, but it will cause a lot of file I/O overhead.

Finally, we used the redis queue to achieve. The number of products to be promoted is stored in redis in a queue. Whenever a user grabs a promotional product, a data is deleted from the queue to ensure that the product will not be oversold. This operation is very convenient and extremely efficient, and finally we adopt this method to achieve

17. The realization of the mall spike?

Answer: Panic buying and flash sales are a very common application scenario nowadays. There are two main problems that need to be solved:

1 The pressure of high concurrency on the database

2 How to solve the correct reduction of inventory in a competitive state ("oversold" problem)

For the first question, it is easy to think of using caching to handle panic buying and avoid directly operating the database, such as using Redis. The second question, we can use the redis queue to complete, put the products to be killed in the queue, because the pop operation is atomic, even if many users arrive at the same time, it is executed in sequence, file locks and transactions are under high concurrency The performance drops quickly. Of course, other things must be considered. For example, the snap-up page is made static, and the interface is called through ajax. There may also be a situation where a user grabs multiple times. At this time, you need to add a queuing queue and Panic buying result queue and inventory queue. In the case of high concurrency, the user is entered into the queue, and a thread is used to remove a user from the queue to determine whether the user is already in the queue for the result of the purchase. If it is, the purchase has been made, otherwise the purchase is not made, the inventory is reduced by 1, and the database is written. Put users in the result queue.

To be continued~~~~~

For more relevant interview questions, you can also follow the WeChat public account: "PHP Great God" Reply to "Interview Manual" and download directly.

Guess you like

Origin blog.51cto.com/15115111/2667914