14

1. (int) why '0xff' is 0?

0xff is string string to int The first one is 0, the conversion can be successful, xff conversion fails

2. Convert between decimal and hexadecimal in PHP

 #10转16
dechex(52558);
或
gmp_strval(52558,16);

#16转10
hexdec('0xcd4e');

3. Query the top ten records of cup and memory through the aux command

 ps -aux --sort -pcpu,-pmem | head -n 10

 pcpu是CPU利用率
 pmem是内存利用率
 --sort -是降序,+是升序

4. What are the basic principles of object orientation?

  1. Single Responsibility Principle SRP (Single Responsibility Principle): It means that the function of a class should be single and cannot be all-encompassing.
  2. Open-Close Principle OCP (Open-Close Principle) A module should be open for extensibility and closed for change.
  3. Substitution Principle (the Liskov Substitution Principle LSP): Subclasses should be able to replace the superclass and appear wherever the superclass can.
  4. Dependency Inversion Principle (the Dependency Inversion Principle DIP) Concrete depends on abstraction, and the upper layer depends on the lower layer.
  5. Interface Segregation Principle (the Interface Segregation Principle ISP) Modules should be isolated through abstract interfaces, rather than strongly coupled through concrete classes.

5. Design pattern classification, design principles

Creation, Structural, Behavioral

Reference: https://github.com/jiayisheji/blog/issues/2

  • A creational pattern is a pattern for creating objects, abstracting the process of instantiation.

    • Simple Factory
    • Factory Method Pattern (Factory Method)
    • Abstract Factory Pattern (Abstract Factory)
    • Creator Mode (Builder)
    • Prototype pattern (Prototype): Use prototype instances to "clone" to create new objects.
    • Singleton pattern (Singleton)
  • Structural Patterns: Handle composition between classes or objects. It is to solve how to assemble existing classes and design their interaction, so as to achieve certain functional purposes.

    • Facade Mode/Facade Mode (Facade Facade Mode)
    • Adapter mode (Adapter)
    • Proxy mode (Proxy)
    • Decorator
    • Bridge Mode (Bridge)
    • Composite mode:
    • Flyweight
  • Behavioral patterns relate to algorithms and the assignment of responsibilities between objects. Behavioral patterns describe patterns of objects and classes, as well as patterns of communication between them.

    • Template Method Pattern (Template Method)
    • Observer pattern (Observer)
    • State mode (State)
    • Strategy mode (Strategy)
    • Chain of Responsibility
    • Command mode (Command)
    • Visitor pattern (Visitor)
    • Mediator Mode (Mediator)
    • Memento
    • Iterator mode (Iterator)
    • Interpreter Mode (Interpreter)

6. The process of a PHP web request

  1. Browser sends URL request
  2. DNS resolution domain name
  3. After obtaining the IP, start to access the IP and Port
  4. Development to send TCP requests to Nginx
  5. Nginx parses the server name and sends it to the php-fpm management process
  6. php-fpm gives specific requests to PHP for execution

7. How many static variables are there in PHP?

Static variables belong to the static storage mode, and their storage space is the static data area in the memory (the storage unit is allocated in the static storage area).

Static variables can be applied wherever they can be applied, and once the application is successful, it will no longer accept other identical applications.

The value of a static variable is mutable, but does not change when the function is called and exited.

Static local variable : An internal variable of static type is a variable that can only be used in a specific function, but always occupies storage space.

The initial value of static variables is 0.

Static global variable : The declaration of a global variable (external variable) is preceded by static to constitute a static global variable.

7. What is the difference between local variables and static local variables, global variables and static global variables?

After changing a local variable to a static variable, it changes its storage method, that is, changes its lifetime.

Changing a global variable to a static variable changes its scope ( static global variables are only valid within the source file where the variable is defined ), limiting its scope of use.

Detailed explanation of static static variables in PHP

9. What is the difference between array_merge and array+array?

 $a=[0,1,2,3]; $b=[1,2,3,4,5]; $a+=$b; echo json_encode($a);//[0,1,2,3,5]

array_merge() will not overwrite the original value

Array+array merging arrays will return the first value as the final result, discarding the values with the same key name in the following arrays.

10. foreach reference pass-by-value result

 <?php
$a=[1,2,3]; 
foreach($a as &$v){} //$v 仍然引用到最后一项 $a[2]
foreach($a as $v){}  //$a[2]会遍历$a中的值,$v=1,$v=2,$v=2
echo json_encode($a);//[1,2,2]

Reference: https://www.php.net/manual/en/control-structures.foreach.php

11. The execution process of PHP?

  1. Scanning: Turn the content of index.php into language fragments (token)
  2. Parsing: turning language fragments into meaningful expressions
  3. Compile (complication): compile expressions into intermediate code (opcode)
  4. Execution: execute the intermediate code one by one
  5. Output (output buffer): output the content to be output to the buffer

12. PHP variable storage

Variables in php exist in the zval variable container.

zval structure: variable type, variable value, is_ref field, refcount field.

  • is_ref: is a bool value used to distinguish whether the variable belongs to the reference set, indicating whether the variable has more than one alias.
  • refcount: Counter, indicating the number of variables pointing to this zval variable container.
  • When the refcount value is 1, the value of is_ref is false. Because the refcount is 1, this variable cannot have multiple aliases, so there are no references.
  • When assigning a variable = to another variable, it does not allocate memory space for the new variable immediately, but adds 1 to the refcount in the zval of the original variable.
  • When the original variable or changes, the memory space will be allocated for the new variable, and the refcount of the original variable will be reduced by 1.
  • If the original variable is unset, the new variable uses the original variable's zval instead of reassigning it.
  • When the & reference is assigned, the is_ref of the original variable becomes 1, and the refcount is incremented by 1. If you assign a value to a variable &, the variable previously assigned with = will allocate space.

13. PHP circular reference memory leak

 <?php
$a = array( 'one' );
$a[] = &$a;
xdebug_debug_zval( 'a' );//a:(refcount=2, is_ref=1),
?>

Before PHP5.2, if the refcount was 0, the variable space could be released, otherwise it would not be released

 unset($a); //refcount减1变为1,不会回收

14. GC garbage collection mechanism of PHP5.3

The algorithm puts all possible roots (possible roots are zval variable containers) in the root buffer (called suspected garbage), and when the root buffer is full, executes all different variable containers inside the buffer. Garbage collection operations.

Or we use gc_collect_cycles in the script to force garbage collection in the buffer.

  1. If a reference count refcount is incremented, it will continue to be used and of course not be in the garbage anymore.
  2. If the reference count refcount is reduced to 0, the variable container will be cleared (free) and will not enter the buffer.
  3. A garbage cycle occurs only when the reference count decreases to a non-zero value, putting it into the buffer. During a garbage cycle, find out which parts are garbage by checking if the reference count is decremented by 1, and by checking which variable containers have zero references.

Or we use gc_collect_cycles() in the script to force garbage collection in the buffer.

15. PHP enable/disable garbage collection

By default, PHP's garbage collection mechanism is turned on, and there is a php.ini setting that allows you to modify it: zend.enable_gc

Use gc_enable() and gc_disable() to turn it on and off in the program

16. XSS attack and defense mechanism

Xss(cross-site scripting) Cross-site scripting attack : Refers to the attacker inserting malicious html tags or javascript codes into web pages.

Defense Mechanisms:

  • Escape tags (htmlspecialchars)
  • Restricted characters (reg\_match)
  • filter(preg\_replace)
  • cookie set HTTPOnly (js cannot read)
  • template engine

17. CSRF attack and defense mechanism

CSRF Cross-Site Request Forgery:

The attack principle and process of CSRF attack are as follows:

  1. User C opens the browser, visits the trusted website A, and enters the user name and password to request to log in to the website A;
  2. After the user information is verified, website A generates cookie information and returns it to the browser. At this time, the user successfully logs in to website A, and the request can be sent to website A normally;
  3. Before the user exits website A, in the same browser, open a TAB page to visit website B;
  4. After the website B receives the user request, it returns some offensive codes and sends a request to visit the third-party website A;
  5. After receiving these offensive codes, the browser sends a request to website A according to the request of website B, carrying the cookie information without the user's knowledge. Website A does not know that the request is actually initiated by B, so it will process the request with C's authority according to user C's cookie information, resulting in the execution of malicious code from website B.

Defense against CSRF attacks: At present, there are three main strategies for defense against CSRF attacks: verifying the HTTP Referer field; adding a token to the request address and verifying; customizing attributes in the HTTP header and verifying.

18. There is a file ip.txt, each line contains one ip record, a total of several lines, how to count the top 3 ips with the most occurrences and their times?

 sort -nr ip.txt | uniq -c | sort -nr | head -n 3

uniq -c filename用于去除冗余并统计每一行出现的次数。 
sort -r 指逆序排序 -n指按数字字符串大小排序 
head -n 指定数量

Note: For the first sorting, ip is arranged in order, because the second uniq will only merge adjacent items. The second sorting is to arrange the ip in order of appearance from large to small and finally take the first three results.

19. The structure of Mysql B-Tree and B+Tree?

B-Tree:

  1. d>=2, that is, the degree of B-Tree (for a node, there are n edges connected to it, it is called degree =n);
  2. h is the height of the B-Tree;
  3. Each non-leaf node consists of n-1 keys and n pointers, where d<=n<=2d;
  4. Each leaf node contains at least one key and two pointers, at most 2d-1 keys and 2d pointers, and the pointers of the leaf nodes are all NULL;
  5. All leaf nodes are at the same level, and the depth is equal to the tree height h;
  6. The key and the pointer are spaced apart from each other, and the two ends of the node are pointers;
  7. The keys in a node are arranged in increasing order from left to right;
  8. dB-TreeNkey ,则其树高h的The upper limit is logd((N+1)/2) , and one key is retrieved, and the asymptotic complexity of finding the number of nodes is O(logdN)

B+Tree:

  • The upper limit of pointers per node is 2d instead of 2d+1 (the number of pointers is the same as the number of key ).
  • Non-leaf nodes do not store data , only store key ;
  • Leaf nodes do not store pointers.

20. Regular Expression Engine

NFA, expression dominant engine

DFA, the text-dominant engine

DFA engine searches are faster. But NFA is dominated by expressions and is easier to manipulate, so general programmers prefer NFA engines

21. Difference between GET and POST

  • GET requests can only be url encoded, while POST supports multiple encoding methods.
  • The parameters passed in the URL for GET requests are limited in length, while POST does not.
  • For parameter data types, GET only accepts ASCII characters, while POST has no restrictions.
  • GET is less secure than POST because the parameters are exposed directly on the URL, so it cannot be used to pass sensitive information.
  • GET parameters are passed through the URL, and POST is placed in the Request body.
  • GET produces one TCP packet, and POST produces two TCP packets.

Reference: https://blog.csdn.net/happy_xiahuixiax/article/details/72859762

22. Reference standard for Mysql optimizer

The use of mysql for indexes is determined by the optimizer of mysql's server layer

23. Memcache and Redis single key size limit

The data stored in a single key (variable) in Memcache has a limit of 1M

The data stored in a single key (variable) of Redis has a limit of 1G

24. Handling of cache avalanches

Beforehand: perform system stress testing, perform current limiting processing at the load balancing layer, discard requests due to overload, or enter the queue

Beforehand: redis high availability, master-slave + sentinel, redis cluster, to avoid complete collapse.

In the process: local cache + current limit downgrade to avoid MySQL being killed.

After the fact: redis is persistent, and once restarted, data is automatically loaded from disk to quickly restore cached data.

25. Distributed id algorithm?

Snowflake algorithm is an open source distributed id generation algorithm of twitter. It is implemented in Scala language. It uses a 64-bit long id, 1 bit is not needed, 41 bits are used as the number of milliseconds, and 10 bits are used as the working machine. id, 12 bit as serial number.

1 bit: No, why? Because the first bit in binary is negative if it is 1, but the ids we generate are all positive, so the first bit is uniformly 0.

41 bit: Indicates the timestamp in milliseconds. 41 bits can represent numbers up to 2^41 - 1, that is, it can identify 2^41 - 1 millisecond value, and when converted into an adult, it means 69 years.

10 bit: Record the working machine id, which means that the service can be deployed on up to 2^10 machines, that is, 1024 machines. But in 10 bits, 5 bits represent the computer room id, and 5 bits represent the machine id. It means that it can represent up to 2^5 computer rooms (32 computer rooms), and each computer room can represent 2^5 machines (32 machines).

12 bit: This is used to record different ids generated within the same millisecond. The largest positive integer that 12 bit can represent is 2^12 - 1 = 4096, which means that the number represented by this 12 bit can be used to distinguish the same millisecond 4096 different ids within.

26. Redis memory elimination mechanism

  • noeviction: When the memory is not enough to accommodate the newly written data, the new write operation will report an error, this is generally not used, it is really disgusting.
  • allkeys-lru: When the memory is not enough to accommodate newly written data, in the key space, remove the least recently used key (this is the most commonly used).
  • allkeys-random: When the memory is not enough to accommodate newly written data, in the key space, a key is randomly removed. This is generally not used. Why is it random? It must be to kill the least recently used key. .
  • volatile-lru: When the memory is not enough to accommodate newly written data, in the key space with the expiration time set, remove the least recently used key (this is generally not suitable).
  • Volatile-random: When the memory is not enough to accommodate newly written data, a key is randomly removed in the key space with the expiration time set.
  • volatile-ttl: When the memory is not enough to accommodate newly written data, in the key space with the expiration time set, the key with an earlier expiration time will be removed first.

27. Common MQ selection

ActiveMQ is developed based on Java, and RabbitMQ is developed based on erlang.

characteristic ActiveMQ RabbitMQ RocketMQ Kafka
Single machine throughput Ten thousand, one order of magnitude lower than RocketMQ and Kafka Same as ActiveMQ 100,000 level, supporting high throughput 100,000 level, high throughput, generally used with big data systems for real-time data calculation, log collection and other scenarios
The impact of the number of topics on throughput Topics can reach the level of hundreds/thousands, and the throughput will decrease slightly. This is a major advantage of RocketMQ. Under the same machine, it can support a large number of topics. When there are dozens to hundreds of topics, the throughput will drop significantly. Under the same machine, Kafka tries to ensure that the number of topics is not too large. If you want to support large-scale topics, you need to add more machine resources.
Timeliness ms level Microsecond level, which is a major feature of RabbitMQ, with the lowest latency ms level Latency is within ms
Availability High, based on master-slave architecture to achieve high availability Same as ActiveMQ Very high, distributed architecture Very high, distributed, multiple copies of one data, few machines down, no data loss, no unavailability
message reliability There is a lower probability of missing data Basically not lost After parameter optimization configuration, 0 loss can be achieved Same as RocketMQ
Feature support The functions in the MQ field are extremely complete Developed based on erlang, with strong concurrency capability, excellent performance and low latency MQ has relatively complete functions, is still distributed, and has good scalability The functions are relatively simple, mainly supporting simple MQ functions, and are used on a large scale for real-time computing and log collection in the field of big data

28. Tree data structure classification

Definition of ordered tree: If the subtrees of each node in the tree are regarded as ordered from left to right (that is, they cannot be interchanged), then the tree is called an ordered tree (Ordered Tree)

Definition of an unordered tree: If the subtrees of each node in the tree are unordered from left to right (that is, interchangeable), the tree is called an unordered tree

29. Detailed understanding of data, data elements, data items, data objects

1. Data: Data is some symbols that the user inputs to the computer and is processed by the computer program, such as pictures and sounds, etc. . . .

2. Data Element: It is the basic unit of data. The data element is used to completely describe an object, such as a student table. The student table is also composed of data elements and data items.

3. Data item (Data item): It is composed of data elements! For example, data items such as "student number, name, gender" in the student table.

4. Data objects: a collection of data elements with the same nature, a subset of data, for example: a collection of integer data objects N={1,2,3,4,5,6,7,...};

30. What is the difference between 301 and 302 jumps? How does PHP display 301, 302, 403, 404 jumps?

301 Moved Permanently Permanently redirect
302 Moved Temporarily Temporary redirect (POST to GET)
307 Temporary Redirect Temporary redirect (keep POST)

301 is generally used as a permanent jump, unless the user clears the browser cache, the jump address will not be modified;
302 and 307 can modify the jump address in the backend, the difference is that 302 will convert POST to GET request, 307 can keep POST

 //301跳转
header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.baidu.com");
//302
Header("Location: http://www.baidu.com");
//403
header('HTTP/1.0 403 Forbidden');
//404
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");

31. What are the conditions for the use of compound indexes?

The compound index will only use the latter compound index when the former field is an exact query. Once an inexact query occurs, the compound index will not be used.

 select * from test where a=10 and b>10 order by c

A_b index is used, order by c does not use index

32. Which step takes the longest time from the execution of the sql statement to the return of the result?

The larger the data volume of the query result, the longer the return time, which far exceeds the time occupied by other links.

33. PHP Mode Modifiers

Mode Modifier Meaning

i

Regular expression matching is case insensitive

m

When m is not added, the matched string is treated as a whole line, ^ matches the start position, $ matches the end position or matches the last newline;

When adding m, the matched string is treated as multiple lines by newline, and each line is matched with the regular enclosed by ^ and $

s

A period (.) in a regular expression will represent any character, including newlines

x

Null characters except escaped in regular expressions, other null characters will be ignored

e

Only used in regular replacement functions such as preg_replace(), which means to replace the content with a function. This modifier is no longer used in higher versions of php and has been replaced by preg_replace_callback()

A

When matching, it will match from the beginning of the string

D

When D is not added, $ matches the end position or matches the last newline;

When adding D, only the end position is matched;

Modifier D is ignored if modifier m is set

U

When U is not added, it is a greedy match, and the maximum number of matching parts will be found;

When adding U, it is a non-greedy match, and only the smallest matching part is found

34. Common methods and functions of HTTP

  • For a server to be compatible with HTTP 1.1, it only needs to implement GET and HEAD methods for resources
  • GET is the most common method and is usually used to request the server to send a certain resource.
  • HEAD is similar to GET, but the server returns the header in the response, not the body of the entity
  • PUT tells the server to use the body of the request to create a new document named after the requested URL, or, if that URL already exists, to replace it with the body
  • POST was originally used to input data to the server. In fact, it is often used to support HTML forms. The data filled in the form is usually sent to the server, which then sends it where it is going.
  • TRACE will initiate a loopback diagnosis on the destination server, and the server at the last stop will bounce back a TRACE response with the original request message it received in the response body. The TRACE method is primarily used for diagnostic purposes, to verify that the request traverses the request/response chain as intended.
  • The OPTIONS method asks the web server to tell it about the various functions it supports. You can query which methods the server supports or which methods are supported for some special resources.
  • DELETE requests the server to delete the resource specified by the request URL

35. Common header request header

- - Example
vary Tell the proxy server/cache/CDN how to judge whether the request is the same Vary: Accept-Encoding,User-Agent
Rang Request a segment of memory, such as 0 to 2000 bytes, that can be used for breakpoint downloads Rang bytes=0-2000
Referer source address
Upgrade switch protocol version Upgrade: HTTP/2.0, SHTTP/1.3
User-Agent User Info User-Agent: Mozilla/5.0 (Linux; X11)
X-Requested-With null traditional request; XMLHttpRequest Ajax request

Reference: https://www.cnblogs.com/yunlongaimeng/p/10904558.html

36. Data structures used by external sorting

External sorting refers to the sorting of large files, that is, the records to be sorted are stored in the external memory, and the files to be sorted cannot be loaded into the memory at one time. Purpose.

The most commonly used algorithm for external sorting is multi-way merge sorting , that is, the original file is divided into multiple parts that can be loaded into memory at one time, and each part is transferred into the memory to complete the sorting. Then, merge-sort the already sorted subfiles. In large-scale data storage, under the actual background of implementing index query, the number of elements stored in tree nodes is limited (if the number of elements is very large, the search will degenerate into a linear search inside the node), which leads to a binary search tree. Due to the large depth of the tree, the disk I/O read and write is too frequent, which leads to low query efficiency. So how to reduce the depth of the tree (of course, it cannot reduce the amount of data queried), a basic idea is: use multi-fork Tree structure (since the number of tree node elements is limited, the number of subtrees of the node is naturally limited). In this way, we propose a new search tree structure - multi-way search tree. According to the inspiration of balanced binary tree, it is natural to think of balanced multi-way search tree structure, that is, B-tree (B tree structure)

37. Three modules of PHP

kernel, zend engine, and extension layers

Reference: [PHP kernel]()

38.What is opcode?

opcode is the intermediate code compiled by Php script, Zend engine converts the source file into opcode code, and then runs it on the virtual machine

Cached opcodes can speed up your website

php opcode can be cached with apc or xcache cache

39. How to convert letters to binary?

The ASCII code of A is 65, hexadecimal corresponds to 41, and binary corresponds to 01000001

The ASCII code of a is 97, hexadecimal corresponds to 61, and binary corresponds to 01100001

40. What is the difference between Apache and Nginx?

  • Nginx is lightweight and takes up less memory and resources than apache
  • Apache is a synchronous multi-process model, one connection corresponds to one process; nginx is asynchronous, and multiple connections (ten thousand levels) can correspond to one process

41. PHP magic methods, magic constants, super global variables

magic method

 __construct 
__destruct 
__call 
__callStatic 
__get 
__set 
__isset 
__clone 
__unset 
__sleep 
__wakeup 
__toString 
__invoke //反射:当尝试以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用。
__set_state

Magic constants: The so-called magic constants are some constants predefined by PHP, and these constants will change with the location.

 __LINE__ 获取文件中的当前行号。
__FILE__ 获取文件的完整路径和文件名。
__DIR__  获取文件所在目录。
__FUNCTION__ 获取函数名称(PHP 4.3.0 新加)。
__CLASS__  获取类的名称(PHP 4.3.0 新加)。
__METHOD__ 获取类的方法名(PHP 5.0.0 新加)。
__NAMESPACE__ 当前命名空间的名称(区分大小写)。
__TRAIT__ Trait 的名字(PHP 5.4.0 新加)。自 PHP 5.4 起此常量返回 trait 被定义时的名字(区分大小写)。Trait 名包括其被声明的作用区域(例如 *Foo\Bar*)。

superglobal variables (9)

 $GLOBALS :储存全局作用域中的变量
$_SERVER :获取服务器相关信息
$_REQUEST :获取POST和GET请求的参数
$_POST : 获取表单的POST请求参数
$_GET : 获取表单的GET请求参数
$_FILES :获取上传文件的的变
$_ENV : 获取服务器端环境变量的数组
$_COOKIE:获取浏览器的cookie
$_SESSION : 获取session

42. Directory for new Linux user profiles

The /etc/skel/ directory is a directory used to store new user configuration files. When we add a new user, all files in this directory will be automatically copied to the home directory of the newly added user. All files in this directory are hidden files (files starting with .dot).

43. What is the difference between national standard code, area code, machine code and machine code?

[GB code] refers to the national standard Chinese character code: GB-2312

[Location code] The location code reserves some vacancies in GB-2312, which is convenient for supplementation and expansion.

[Internal code] Chinese character ASCII code. Refers to the code composed of 0 and 1 symbols used in the internal storage of the computer, processing and transmission of Chinese characters.

[Machine code] The programming language directly used by the computer, and its statement is the machine instruction code.

44. PHP file pointer operation?

 ftell(resource handle)         //返回文件指针的当前位置
fseek(resource hanlde,int offset[,int whence])          //移动文件指针到指定位置
rewind(resource handle)          //移动文件指针到文件的开头
feof() //测试文件指针是否在结尾

45. Briefly describe HTTP and TCP/IP protocols

  1. HTTP Hypertext Transfer Protocol, an application layer protocol, mainly solves how to package data, and is based on TCP connection. TCP/IP Transmission Control Protocol is a transport layer protocol, which mainly solves the problem of how data is transmitted in the network.
  2. The HTTP protocol is an application layer protocol based on the request and response mode for obtaining addresses and transferring data. And TCP is an interactive mode where server and client can send messages to each other. The server can actively push messages to the client.
  3. Http just closes the TCP connection after each request is completed, so it is a short connection. TCP is a persistent connection.

46. Composition of HTTP messages

Http packets include two parts: request packets and response packets :

  • Request message:

    • request line: request type, resource accessed, HTTP version
    • Request header (header): informs the server about the client's request
    • Empty line: Indicates that the request header has ended
    • Request body: data that can carry multiple request parameters
  • Response message:

    • status line
    • response header
    • blank line
    • response body

Reference: https://www.cnblogs.com/xingxia/p/10028240.html

47. Advantages of HTTP 1.1

  1. Support long connection: Default Connection: keep-alive When a web page is opened, the TCP connection used to transmit HTTP data between the client and the server will not be closed. If the client accesses the web page on the server again, it will continue to use this method. An established TCP connection.
  2. Multiple HTTP requests and responses can be transmitted on one TCP connection, reducing the consumption and delay of establishing and closing connections, that is, reducing the number of TCP slow starts
  3. Allows the client to issue the next request without waiting for the result of the previous request to return, reducing the number of RTTs and the time required for the download process
  4. Provides request and response headers related to mechanisms such as authentication, state management, and Cache.
  5. Security performance is not a feature of HTTP1.1, but the use of HTTPS solves the security problem of HTTP, namely Secure Hypertext Transfer Protocol (SHTTP).

48. What is the difference between a method starting with mb_ and a method starting with non-mb in PHP?

String functions starting with mb, the processed strings can be multi-byte

Such as strlen function, UTF8 Chinese is 3 bytes long, mb_strlen is 1 byte length

49. The difference between str_replace and substr_replace

str_replace() : function replaces some characters in a string (case sensitive)

substr_replace(): Replace part of a string with another string at a specified position

 <?php
echo substr_replace("Hello","world",0); //输出 world
// 0 will start replacing at the first character in the string

50. Briefly describe the TCP and UDP protocols

TCP: Transmission Control Protocol

UDP: User Datagram Protocol

1. udp is connectionless, tcp is connection-oriented;

2, udp is unreliable transmission, tcp is reliable transmission;

3. udp is for message transmission, and tcp is for byte stream transmission.

What is the difference between tcp and udp

51. TCP RTTs: Weighted Average Round Trip Time

TCP uses an adaptive algorithm that records when a segment is sent and when the corresponding acknowledgment is received. The difference between these two times is the round-trip time RTT of the segment.

Timeout retransmission time RTO

RTTD is the weighted average of the deviations of the RTT

52.awk usage

Ancient Artifact AWK

53.inode

When the operating system reads the hard disk, it does not read sector by sector, which is too inefficient. Instead, it continuously reads multiple sectors at one time, that is, reads one "block" at a time. This "block" composed of multiple sectors is the smallest unit of file access. The size of the "block", the most common is 4KB, that is, eight consecutive sectors form a block.

The area where the metadata of the file is stored is called inode, which is translated as "index node" in Chinese.

The inode contains meta information about the file, specifically the following:

  • number of bytes of the file
  • User ID of the file owner
  • Group ID of the file
  • File read, write and execute permissions
  • There are three timestamps of the file: ctime refers to the time when the inode was last changed, mtime refers to the time when the content of the file was last changed, and atime refers to the time when the file was last opened.
  • The number of links, i.e. how many filenames point to this inode
  • The location of the file data block

All file information except the file name is stored in the inode

You can use the stat command to view the inode information of a file:

stat example.txt

54. The difference between file() and file_get_contents()

The file() function reads the entire file into an array. Each cell in the array is the corresponding line in the file, including newlines. Return false on failure.

The file_get_contents() function reads the entire file into a string. Return false on failure.

55. PHP's singleton pattern application scenario

A single page-level request occurs when multiple application scenarios occur and the same object resources need to be shared.

Such as database operation classes, network request classes, log operation classes, configuration management services, etc.

 class Singleton
{
    private static $uniqueInstance;
    private $singletonData = '单例类内部数据';
    private function __construct()
    {
        // 构造方法私有化,外部不能直接实例化这个类
    }
    public static function GetInstance()
    {
        if (self::$uniqueInstance == null) {
            self::$uniqueInstance = new Singleton();
        }
        return self::$uniqueInstance;
    }
    public function SingletonOperation(){
        $this->singletonData = '修改单例类内部数据';
    }
    public function GetSigletonData()
    {
        return $this->singletonData;
    }
}

Let's talk about the singleton pattern in PHP

56. PHP socket

Socket is an abstraction layer between the application layer and the transport layer. It abstracts the complex operations of the TCP/IP layer into several simple interfaces, which are called by the application layer to realize the communication between processes in the network.

  • Client process: create Socket, connect to server, connect Socket to remote host (note: only TCP has the concept of "connection", some Sockets such as UDP, ICMP and ARP do not have the concept of "connection"), send data, read Respond to data until the data exchange is complete, close the connection, and end the TCP conversation.
  • Server-side process: First initialize Socket, establish a streaming socket, bind with the local address and port, then notify TCP, ready to receive connections, call accept() to block, and wait for a connection from the client. If the client establishes a connection with the server at this time, the client sends a data request, the server receives the request and processes the request, and then sends the response data to the client, and the client reads the data until the data exchange is completed. Finally, the connection is closed and the interaction ends.

What is socket in php?

57. PHP array structure and sorting principle

PHP array is implemented by hash table + doubly linked list

Sorting principle:

  1. Apply for n additional spaces
  2. Traverse the double linked list and call the sorting function zend\_qsort (internally a quick sort algorithm) to sort the array
  3. Call the sorting function zend\_qsort (internally a quick sort algorithm) to sort the array
  4. After sorting, the position of the node in the doubly linked list changes, so the specified pointer is adjusted
  5. Traverse the array and set the pListLast and pListNext of each node respectively
  6. Set pListTail of HashTable

58. PHP language structure and functions

  • Language structure: it is the keyword of the PHP language, part of the language grammar
  • Function: It is composed of code blocks and can be reused. Functions must first be decomposed into language structures by the PHP parser (Zend engine), with an additional layer of parser parsing.

List of language structures:

echo() print() die() isset() unset() include() array() list() empty() require(), note that include_once() is a function and require_once() is a function

59. The operating mode and advantages and disadvantages of PHP-FPM?

  • static : Indicates that pm.max_chindren child processes are directly forked when php-fpm is running,
  • dynamic: Indicates that start_servers processes are forked at runtime, and dynamically adjusted according to the load, and no more than max_children processes at most.

It is generally recommended to use static. The advantage is that there is no need to dynamically judge the load situation to improve performance; the disadvantage is that it takes up more system memory resources.

N is the number of CPU cores, M is the amount of memory PHP can utilize, and m is the average amount of memory used by each PHP process

Formula for dynamic mode: between N + 20% and M / m

Formula in static way: M / (m 1.2)

pm.max_requests: Refers to how many requests each child process restarts after processing. This parameter can theoretically be set arbitrarily, but in order to prevent the risk of memory leaks, it is better to set a reasonable number.

60. PHP common information functions

 phpinfo — 输出关于 PHP 配置的信息
phpversion — 获取当前的PHP版本
php_sapi_name — 返回 web 服务器和 PHP 之间的接口类型
ini_get — 获取一个配置选项的值
ini_get_all — 获取所有配置选项
ini_restore — 恢复配置选项的值
ini_set — 为一个配置选项设置值

61. Shell basic operations

 变量赋值:变量名=值                                如 FRUIT=apple
取变量值:在变量名前加上 $,这在shell中一般是取变量值的意思
算术表达式求值:$(( ... ))                            如  echo $((a + b))    
字符串比较: -n 是否不为空 -z 是否为空         如 if [ -n "$str1" ]

62. Algorithm, logical structure, relationship of storage structure

The design of an algorithm depends on the logical structure chosen, and the implementation of the algorithm depends on the storage structure used.

  • Sequential storage structure: The logical structure (relationship) between data elements is represented by the relative position of the data elements in memory.
  • Chained storage structure: Add a pointer (pointer) to store the address of another element in each data element, and use this pointer to represent the logical structure (relationship) between data elements

63. PHP pseudo-types

Fake types: Fake types, types that don't actually exist in PHP. However, pseudo-types can help programmers to better view the operation manual and make it easier to learn.

  1. mixed type: multiple data types in PHP
  2. numberNumeric type: any numeric type (integer and float)
  3. callback callback type: callback function as parameter

64. Regular matchers


65.The role of ARP and RARP protocol in TCP/IP protocol?

The role of the ARP protocol is to find the corresponding MAC address from the IP address.

The role of the RARP protocol is just the opposite, that is, the corresponding IP address is searched by the MAC address.

66. Replay attack, Smurf attack, dictionary attack, man-in-the-middle attack

  • Replay attack means that the attacker sends a packet that the destination host has received to deceive the system. It is mainly used in the identity authentication process and destroys the correctness of the authentication. A replay attack can be carried out by the originator or by the place that intercepts and resends that data.
  • A Smurf attack is a distributed denial of service (DDoS) attack that uses a combination of IP spoofing and ICMP reply methods to flood the target system with a large amount of network traffic, causing the target system to deny service to normal systems. The Smurf attack floods the victim host by using the ICMP reply request packet with the reply address set to the broadcast address of the network, and finally causes all the hosts in the network to reply to the ICMP reply request, resulting in network congestion. More complicated to change the source address to the victim of a third party, which eventually causes the third party to crash.
  • Dictionary attack is an attack method in which possible passwords in a user-defined dictionary are tried one by one when cracking a password or key. The difference from brute force is that brute force tries all possible password combinations one by one, while dictionary attack uses a predefined list of words.
  • A man-in-the-middle attack is an indirect intrusion attack, in which a computer controlled by an intruder is virtually placed between two communicating computers in a network connection through various technical means. This computer is called "man-in-the-middle". ". By intercepting normal network communication data, and performing data tampering and sniffing, both parties of the communication are unaware.

71. EXT3, EXT4, XFS file system size?

Linux file system size single file size
ext3 16TB 2TB
ext4 1EB 16TB
xfs 18EB 9EB

72. Linux system file descriptor:

0: STDIN standard input

1: STDOUT standard output

2: STDERR standard error output

73.Mysql log

  1. How many kinds of log redo/undo are there
  2. log storage format

    1. redo: When the page is modified, it is first written to the redo log buffer, then written to the file system cache of the redo log (fwrite), and then synchronized to the disk file (fsync).
    2. Undo: Before MySQL 5.5, undo can only be stored in ibdata* files. After 5.6, undo logs can be stored outside ibdata* by setting the innodb\_undo\_tablespaces parameter.
  3. How the transaction is implemented through the log: Because when the transaction modifies the page, it must first record the undo, and before the undo, it must record the redo of the undo, then modify the data page, and then record the redo of the data page modification. Redo (including undo modifications) must be persisted to disk before data pages. When a transaction needs to be rolled back, because of undo, the data page can be rolled back to the state of the previous mirror. During crash recovery, if the transaction in the redo log does not have a corresponding commit record, then it is necessary to use undo to roll back the modification of the transaction to before the transaction begins. If there is a commit record, use redo to roll forward to the completion of the transaction and commit it.

74. Mysql json type

Since 5.7.8, mysql began to support the json data type. When the json data type is stored, the format check will be performed. If the json format is not satisfied, an error will be reported. The default value of the json data type is not allowed to be empty.
Advantage:
  1. The storage is similar to text, which can store very large data.
  2. JSON validity check: The inserted data must be a string of JSON type.
  3. In contrast to the traditional form, it is not necessary to traverse all strings to find data.
  4. Support indexing: Part of the data in JSON can be indexed through the function of virtual columns

Reference: https://blog.csdn.net/u011207553/article/details/88912219

75. Mysql index creation principle

  1. The most suitable columns for indexing are those that appear in the where clause or join clause, not after the select keyword
  2. The larger the cardinality of the index column, the better the index effect.
  3. To index strings, a prefix length should be specified, which can save a lot of index space
  4. Create a compound index according to the situation, compound index can improve query efficiency
  5. Avoid creating too many indexes, which will occupy additional disk space and reduce the efficiency of write operations
  6. Choose the shortest data type as possible for the primary key, which can effectively reduce the disk usage of the index and improve efficiency

76. Notes on Indexing

  1. Compound indexes follow the left prefix principle
  2. Like query, % cannot be first, you can use full-text index
  3. column is null index can be used
  4. If MySQL estimates that using an index is slower than a full table scan, it will abandon the use of the index

77. Reasons for slow query speed

  1. Open the slow query log and analyze it through pt-query-dugest

     #看一下当前mysql数据库是否开启了慢查询
    show variables like 'slow_query%';
    #临时开启
    set global slow_query_log='ON';

    Permanently open: modify the configuration file my.cnf and add it under [mysqld]
    [mysqld]
    slow_query_log = ON
    slow_query_log_file = /var/lib/mysql/tmp_slow.log //linux
    long_query_time = 1

    restart mysql service

  2. show profile, enabled by set profiling=1;, the time consumed by all statements executed on the server will be recorded in the temporary table. show profile for query QUERY_ID query specified query
  3. show status, query some counters and guess which ones are expensive or time consuming
  4. show processlist, query thread status for analysis
  5. explain, analyze a single SQL statement query

78. Mysql optimizes data access during query

  1. Accessing too much data leads to performance degradation
  2. Determine if the application is retrieving more data than is needed, possibly too many columns or rows
  3. Determine if mysql is parsing a large number of unnecessary rows of data
  4. Query unnecessary records, use limit to limit
  5. Outright association returns all columns specified A.id, A.name
  6. Take out all the columns in total, select * will make the optimizer unable to complete all the optimizations covering the scanning code
  7. Repeatedly query the same data, the data can be cached
  8. Change the structure of the database and table, modify the data table paradigm
  9. Rewrite SQL statements so that the optimizer can execute better

79. Optimize long and difficult queries

  1. MySQL can scan millions of rows of data in memory per second. In contrast, responding to data to the client is much slower.
  2. Break a large query into multiple smaller queries
  3. Decomposing an associated query, decomposing an associated query into multiple SQLs for execution, makes the cache more efficient, executing a single query can reduce lock competition, and doing association at the application layer can make it easier to split the database, and the query efficiency will be greatly improved. Improved, queries with less redundant records

80. Optimize specific types of queries

  1. Optimize count() query, count(*) will ignore all columns and count all columns directly, so do not use count(column name)
  2. Optimize the relational query, make sure that there is an index on the column of the ON or USING clause; ensure that there is only one table column in the GROUP BY and ORDER BY, so that MySQL may use the index
  3. Optimized subqueries, can be replaced by associated queries
  4. Optimize GROUP BY and DISTINCT, build indexes for optimization
  5. To optimize LIMIT paging, you can record the maximum ID of the last query. If sorting by id, the next query will query according to this ID (eg: ID > maxID)
  6. Optimize UNION query, UNION ALL has higher performance than UNION

81. Common units with high concurrency

  1. QPS: The number of requests or queries per second, in the Internet domain, the number of requests per second (referring to HTTP requests)
  2. Throughput: The number of requests processed per unit time (usually determined by QPS and concurrency)
  3. Response time: the time it takes from the time the request is sent to the time the response is received
  4. PV: Page View, that is, the number of page views or clicks, the number of pages visited by a visitor within 24 hours. When the same person browses the same page of your website, it is only recorded as PV once
  5. UV: Unique Visitor (UniQue Visitor), that is, the same visitor visits the website multiple times within a certain time range, which can only be calculated

for 1 unique visitor

  1. Bandwidth: To calculate the bandwidth size, you need to focus on two indicators, peak traffic and the average size of the page
  2. Daily website bandwidth = PV/statistical time (seconds) average page size (KB) 8
  3. The peak is generally a multiple of the average
  4. QPS is not equal to the number of concurrent concurrent connections. QPS is the number of HTTP requests per second, and the number of concurrent connections is the number of requests processed by the system at the same time
  5. Twenty-eight law (80% of the traffic is concentrated in 20% of the time): (80% of the total PV ) / (20% of the second speed in 6 hours ) = the peak number of requests per second (QPS)
  6. Stress test: can withstand the maximum number of concurrent and maximum QPS value

82. Common performance testing tools ab

 # 模拟并发请求 100 次,总请求 5000 次 
ab -c 100 -n 5000 http://example.com

83. http status code

  1. 200 The request was successful 2. 204 not content
  2. 206 reset content
  3. 301 Permanent Redirect
  4. 302 temporary redirect
  5. 307 Temporary redirect
  6. 400 Bad Request
  7. 401 Missing authentication information
  8. 403 Denied
  9. 404 does not exist
  10. 500 Server exception
  11. 502 Bad Gateway
  12. 503 Server overload or downtime for maintenance

84. OSI seven-layer protocol

The Internet of Things Communication Form should be

  1. Physical layer: establish, maintain, and disconnect physical connections
  2. Data link layer: establish logical connections, perform hardware address addressing, error checking and other functions
  3. Network layer: logical address addressing, path selection between different networks
  4. Transport layer: defines the protocol port number for transmitting data, first-level flow control and error checking. The protocol is TCP/UDP, and once the data packet leaves the network card, it enters the network transport layer
  5. Session layer: establish, manage, and terminate sessions
  6. Presentation layer: data presentation, security, compression
  7. Application layer: the interface between network services and users, the default protocols are: http(80), ftp(21), tftp, smtp(25), snmp, dns(53), telnet(23), https(443), pop3( 110),dhcp

85. Common request headers/response headers of HTTP protocol

  1. Content-Type specifies the data content type
  2. Accept specifies that the client can accept the data content type
  3. Origin The origin of the original request (POST)
  4. cookies
  5. Cache-Control specifies the requested caching mechanism
  6. User-Agent User browser information
  7. Referrer upper-level request path
  8. X-Forwarded-For requester real ip
  9. Access-Control-Allow-Origin allows other request domains for cross-domain
  10. Last-Modified last response time

IT小马
1.2k 声望166 粉丝

Php - Go - Vue - 云原生