Basic Summary of Junior PHP Interviews

  php

timg.jpeg

This is a set of face-to-face examination questions that I have compiled. The old irons will review it if they look at it.

summarize

I feel that the interview question is somewhat unpopular now, just like the one in Germany yesterday, but I think it should be reviewed in advance. Prepare ahead of time. Please be careful not to suffer heatstroke when you go out for an interview in the past 2 months.

Say a few design patterns you know

singleton pattern

Ensure that a class has only one instance and provide a global access point to it, such as a database connection in the framework-similar to a DB class

Policy mode

For a group of algorithms, each algorithm is encapsulated into an independent class with a common interface. For example, when entering a personal homepage, different displays and operations are given according to different viewers-similar to different users, showing different effects.

Registration mode

Provides for the orderly storage and management of a set of global objects in the program-injection of similar service providers

Adapter mode

Adapting different interfaces into a unified API interface, such as mysql, mysqli, pdo, etc. for data operation, the adapter mode can be used to unify the interfaces

observer mode

An object makes itself observable by adding a method. When the observable object changes, it sends a message to the registered observer. For example, realize message push-register instantiations of all classes into an array, and execute classes in batch through loops

Decorative mode

Dynamically extend the functions of the class without modifying the code and inheritance of the original class. For example, each Controller file of the framework will provide before and after methods-executed before or after a certain method or output for modification

iterator pattern

Provides a method to access each element in an aggregate object in sequence. Iterator class-yield will be inherited in PHP

What is the difference between POST and GET?

  1. GET is to obtain data from the server, POST is to transfer data to the server
  2. GET is received by sending HTTP protocol and passing URl parameters, while POST is entity data and submitted by form.
  3. The amount of data transferred by GET is small and cannot be larger than 2KB. The amount of data transferred by POST is relatively large and is generally set as unrestricted by default.
  4. GET security is very low, POST security is high.

The difference between a session and a cookie.

  1. Session is stored in the php specified directory on the server (session_dir)
  2. Cookie are stored on the client

What are the transactions in the database?

An ordered set of database operations as a unit. If all operations in the group are successful, the transaction is considered successful, even if one operation fails. The transaction is also unsuccessful. If all operations are completed and the transaction commits, its modification will be applied to all other database processes. If one operation fails, the transaction will be rolled back and the operations affected by the transaction will be cancelled.

What is the difference between reference and value in PHP?

Transfer by Value: Changes in the contents of values within a function have no effect on the outside of the function
Reference Transfer: Changes in the contents of values within a function will also be modified outside the function.

What are MYSQL optimization plans?

  1. Select the appropriate field type to avoid unnecessary space increase in the database. Set the field to NOTNULL as far as possible. Similar to gender, provinces try to use enumeration type ENUM.
  2. Use JOIN Query instead of Subquery
  3. Use UNION instead of temporary table
  4. Try not to use foreign keys unless the consistency and integrity between data tables and tables must be maintained.
  5. Try not to use views
  6. Separate tables and libraries, separate reading and writing
  7. Reasonable Setting of Primary Key and Index

    • Indexes are divided into: common index, unique index, full-text index, and primary key (also a unique index)
  8. Optimization of Data Table in Late Evolution

    -vertical splitting: the table data is split into different tables and split according to business.
     -horizontal splitting: the row data is split into different tables, and the tables are split according to factors such as time, type, identity, etc.
     -horizontal and vertical joint split

9. Index building principles

-leftmost prefix matching principle
 -Index columns are excluded from calculations as much as possible
 -Try to expand the index instead of creating a new one

Five basic principles of object-oriented programming (S.O.L.I.D)

  1. SRP Single Function Principle (You can do this, not that you should)

    -never have more than one factor causing class changes, and ensure that classes have and have only one responsibility.
  2. OCP open closed principle

    -Open to expansion and closed to modification
  3. LSP richter replacement principle

    -if an instance of a subclass should be able to replace any instance of its parent class, there is an IS-A relationship between them
  4. ISP interface isolation principle

    -Don't force clients (callers in general) to rely on interfaces they don't use
  5. DIP dependency reversal principle

    • High-level modules should not rely on bottom-level modules, both should rely on their abstractions.
    • Abstraction should not depend on details; details should depend on abstraction.

Use of yield

The generator provides an easier way to implement simple object iteration. Compared with the way of defining classes to implement Iterator interface, the performance overhead and complexity are greatly reduced.

What are the restrictions for subclasses to override the protected method of the parent class?

  1. Final-decorated class methods cannot be overridden by subclasses
  2. 5.3 After that, the number of method parameters must be consistent
  3. When rewriting, the access level can only be equal to or looser than that of the current rewriting method.

What is CGI? What is FastCGI? What is the relationship between php-fpm, FastCGI,Nginx Nginx?

CGI is a universal gateway interface. The interaction between the user’s WEB server and the application program defines the input and output specifications. The user’s request is forwarded to FastCGI process through the WEB server. FastCGI process calls the application program for processing, such as PHP parser. The processing result of the application program, such as HTML, is returned to FastCGI,FastCGI is returned to Nginx for output. Assuming that the WEB server is NGINX, the application is PHP, and php-fpm manages FastCGI, this is the relationship between php-fpm,fastCGI and NGINX.

Fastcgi is used to improve the performance of CGI programs, starting a master and then starting multiple worker. php.ini does not need to be parsed each time. php-fpm implements fastcgi protocol, and is the process manager of fastcgi. It supports smooth restart and can generate multiple processes in advance when starting.

What is CSRF attack? XSS attack? How to prevent?

CSRF, Cross-site Request Forgery, Attacker Fakes User Identity to Send Request to Steal Information or Destroy System.
To guard against

  1. Reasonable specification of api request method, GET, POST
  2. The POST request is verified with a token token, a random code is generated and stored in the session, the random code is carried in the form, and the server verifies whether the random codes are the same when submitting.

XSS, cross-site scripting attack.
To guard against

  1. Don’t believe any input, filter the input.

The basic algorithm

in order to find

function search($arr,$k){
   $n = count($arr);
 $arr[$n]= $k;
 for($i=0;   $i<$n;  $i++){
 if($arr[$i]==$k){
 break;
 }
 }
 if ($i<$n){
 return $i;
 }else{
 return -1;
 }
 }

binary search

function binaryRecursive($arr,$low,$top,$target){
 if($low<=$top){
 $mid = floor(($low+$top)/2);
 if($arr[$mid]==$target){
 return $arr[$mid];
 }elseif($arr[$mid]<$target){
 return binaryRecursive($arr,$mid+1,$top,$target);
 }else{
 return binaryRecursive($arr,$low,$top-1,$target);
 }
 }else{
 return -1;
 }
 }

Bubble sort

function _sort($arr)
 {
 $count = count ($arr) - 1;
 $temp  = 0;
 
 for ($i = 0;   $i < $count;  $i++) {
 
 for ($j = 0;   $j < $count - $i;  $j++) {
 if ($arr[$j] < $arr[$j + 1]) {
 $temp        = $arr[$j];
 $arr[$j]     = $arr[$j + 1];
 $arr[$j + 1] = $temp;
 }
 }
 }
 
 return $arr;
 }

Quick sort

function quick_sort($arr)
 {
 //Judge whether the parameter is an array
 if(!  is_array($arr)) {
 return false;
 }else{
 //Recursive exit: array length is 1, directly return array
 $length = count($arr);
 
 if($length<=1) return $arr;
 //If there are multiple array elements, define two empty arrays
 $left = $right = array();
 //Use the for loop to traverse, taking the first element as the comparison object
 for($i=1;   $i<$length;  $i++)
 {
 //Determine the size of the current element
 if($arr[$i]<$arr[0]){
 $left[]=$arr[$i];
 }else{
 $right[]=$arr[$i];
 }
 }
 //Recursive call
 $left=quick_sort($left);
 $right=quick_sort($right);
 //Combine all results
 return array_merge($left,array($arr[0]),$right);
 }
 }

NOSQL

What is the difference between Redis and Memecached?

  1. Redis supports richer data storage types, String, Hash, List, Set, and Sorted Set. Memcached only supports simple key-value structures.
  2. Memcached key-value storage has a higher memory utilization than Redis uses hash structure to store key-value.
  3. Redis provides the function of transaction, which can ensure the atomicity of a series of commands.
  4. Redis supports data persistence and can keep the data in memory on disk.
  5. Redis uses only single cores, while Memcached can use multiple cores, so on average Redis on each core performs better than Memcached when storing small data.

How does Redis achieve persistence?

  1. RDB persistence saves the state of redis in memory to the hard disk, which is equivalent to backing up the database state.
  2. AOF persistence (Append-Only-File) records the database by saving the write state executed by the Redis server lock. Equivalent to the commands received by the backup database, all commands written to AOF are saved in the protocol format of redis

Laravel design principle

What is the service provider?

The service provider is the center where all Laravel applications are booted. Laravel’s core server, registration service, binding service, listener, middleware, route registration and our applications are all booted by the service provider.

What is the IOC container?

IOC is control inversion, also known as dependency injection (DI). object a can depend on object b, but control is in object a, so it is called control inversion. dependency injection dynamically injects a certain dependency relationship into the object while the IOC container is running.
Function: Solve the difficulty of code and realize low coupling and high expansion.

What are Facades?

A “static” interface is provided to access classes registered in the IoC container. Provides simple, easy-to-remember syntax without remembering long class names that must be manually injected or configured. In addition, due to the unique usage of PHP dynamic methods, it is also very easy to test.

What is the Contract?

Contract is the interface of core services provided by Laravel definition framework. There is no essential difference between Contract and Facades, its function is to make the interface less coupled and simpler.

Architecture

The Basic Principle and Function of Thrift Implementation

Through the use of RPC communication protocol, the non-aware mutual calls in multilingual development scenarios are realized.
Advantages

  • The data packet is sent in binary and the traffic consumption is small.
  • High transmission efficiency
  • No barrier between languages

How to Solve High Concurrency of App Big Data Traffic on Optimized Website

  • Hardware aspect
  • Software aspect
  • No external chain theft
  • Control the download of large files
  • load balancing
  • Distributed
  • Cluster
  • Master-slave database
  • distributed database
  • Distributed cache

TCP three-way handshake

Three-way handshake is the confirmation that the client needs to send 3 packets to establish TCP connection with the server, and the connection can be established after the completion of three-way handshake.

  • First handshake. The client sends a request message, the flag bit SYN is set to 1, the randomly generated seq value is X, the client enters SYN_SENT state, and waits for a response from the server.
  • Second handshake. The server receives the request message, sets SYN and ack to 1, ACK to x+1, randomly generates seq value to Y, then sends the data packet to the client for confirmation, and the server enters SYN_RCVD state.
  • First three-way handshake. After receiving the response data packet, the client confirms whether the ack is x+1 and whether the ack is 1. If it is correct, it sets the ACK to 1 and the ACK to y+1, and then sends the data to the server. After receiving the data, the server side checks whether ack is y+1 and ACK is 1. If it is correct, the connection is established correctly, and both parties enter the ESTAB-LISHED state to complete the three-way handshake.

The difference between TCP and UDP

Besides TCP, we often hear UDP, so what is the difference between them?
TCP corresponds to applications requiring high reliability. As can be seen from the above explanation, three-way handshake is required before real communication, which is connection-oriented. And TCP uses sequence numbers to ensure the ordering of messages.
UDP corresponds to applications with low reliability requirements, but large traffic and fast speed. Compared with TCP, UDP is connectionless and may be out of order.
TCP is heavier, UDP is lighter, there is no difference between high and low, but the application scenarios are different.

Non-technical issues

  1. Why did you leave your last company?
  2. What is the career plan for the next three years?
  3. Do you have anything to understand?

There are no correct answers to non-technical questions.