21. The difference between the statement include and require
Require is included unconditionally, that is, if require is added to a process, require will be executed first regardless of whether the condition is met or not. When the file does not exist or cannot be opened, an error will be prompted and the program execution will be terminated.
Include has a return value, but require does not (maybe because require is faster than include). If the included file does not exist, an error will be prompted, but the program will continue to execute
Note: When the include file does not exist or the syntax is wrong, require is fatal, but include is not
require_once, include_once means to include only once, avoiding repeated inclusion
Twenty-two, the difference between passing value and passing reference in php, and explain when passing value is passed by reference
Variables are always assigned by value by default, that is, when the value of an expression is assigned to a variable, the value of the entire expression is assigned to the target variable, which means: when one variable is assigned to another variable, Changing the value of one of the variables will not affect the other variable
PHP also provides another way to assign values to variables: reference assignment. This means that the new variable simply references (in other words, becomes its alias or points to) the original variable. The changed new variable will affect the original variable and vice versa.
Use reference assignment, simply add an ampersand before the variable to be assigned (source variable)
Objects are passed by reference by default
For larger data, you can consider passing by reference, which can save memory overhead
Twenty-three, PHP does not use the third variable to exchange the values of the two variables
// $a.=$b; $b=str_replace($b,"",$a); $a=str_replace($b,"",$a); // list($b,$a)=array($a,$b); var_dump($a,$b);
Twenty-five, the difference between redis and memache cache
1. Data type
Redis supports multiple data types (5 types): hash string list set zset
memcache only supports key-value
Redis supports two persistence methods: RDB and AOF
memcache does not support persistence
3. Distributed storage
redis supports master-slave replication mode
memcache can use consistent hash for distributed
4. The value is different
memcache is a memory cache, the length of the key is less than 250 characters, and the storage of a single item is less than 1M, which is not suitable for virtual machines.
5. Thread model
memcache is a threading model of master+worker, where the master completes network monitoring and delivers it to the worker thread, which is processed by the worker thread
Redis is a single-process single-threaded model, that is, a single thread completes everything
These two implementations cause the following differences, that is, redis is easier to implement multiple data structures, such as lists, collections, hashes, ordered collections, etc., because it is single-threaded, if redis is deployed in a single instance, the multi-core server cannot be fully used. Advantages, usually when deploying, it will be deployed through multiple instances
6. Memory management
redis: Redis does not have its own memory pool, but directly allocates it when it is used, that is, when it needs to be allocated, and the memory management is left to the kernel, and it is only responsible for fetching and releasing, directly malloc and free. There is no special algorithm for memory management, and memory management (application, release) is done by using google's jmalloc library
memcache: memcached has its own memory pool, that is, pre-allocate a large block of memory, and then allocate memory from the memory pool, which can reduce the number of memory allocations and improve efficiency. This is also the realization of most network servers. However, the management method of each memory pool varies according to the specific situation. The memory management similar to linux is used, that is, the slab memory management method.
Redis supports transactions, channels (publish-subscribe), clusters; memcache does not support
Twenty-six, the advantages and disadvantages of apche and nginx
Nginx is lightweight, occupies less memory and resources than apache, and is resistant to concurrency
Nginx processing requests is asynchronous and non-blocking, while apache is blocking. Under high concurrency, nginx can maintain low resources, low consumption and high performance.
The advantages of apache over nginx:
Rewrite is more powerful than nginx's rewrite, has fewer bugs, and is stable. (Nginx is required for performance, apache is for stability).
27. The parameter of a function cannot be a reference to a variable, unless allow_call_time_pass_reference is set to on in php.ini.
When the PHP function is called, the basic data types will be passed by value by default instead of by reference. The role of the allow_call_time_pass_reference option is whether to enable mandatory parameters to be passed by reference when the function is called. If allow_call_time_pass_reference is configured as on, then the reference pass value will be used by default when the function is called. However, this method is not recommended because it is likely to be no longer supported in future versions. If you want to pass by reference, it is recommended to explicitly use & to pass by reference when calling the function.
28. What is memory management?
Memory management mainly refers to the allocation, use, and release of computer memory resources when the program is running. The goal of memory management is to allocate memory efficiently and quickly while releasing and reclaiming memory resources in a timely manner. Memory management mainly includes whether there is enough memory for the program to use, obtain available memory from the memory pool, destroy it in time after use, and reallocate it to other programs for use.
In the PHP development process, if you encounter operations such as large arrays, it may cause problems such as memory overflow. Some common treatment methods are as follows:
1) Reset the memory size that php can use through the ini_set('memory_limit','64M') method. Generally, the php.ini file cannot be modified on the remote host, but can only be set through the program. Note: in safe_mode (safe mode), ini_set will be invalid.
2) On the other hand, the array can be processed in batches, useless variables can be destroyed in time, and the use of static variables can be minimized. When data reuse is needed, you can consider using references (&). At the same time, the database and file must be closed in time after the operation is completed, and the destructor must be called in time after the object is used.
3) Use the unset() function to release variables in time, and pay attention to the following two points when using:
The unset() function can only release memory space when the variable value occupies more than 256 bytes of memory space.
Only when all the variables pointing to the variable are destroyed, can the memory be successfully released.
Twenty-nine, the characteristics and characteristics of Memcache
1) The agreement is simple.
2) Event processing based on libevent.
3) Built-in memory storage mode.
4) Distributed Memcached does not communicate with each other.
(1) The maximum data of a single item is 1MB.
(2) The maximum memory used by a single process is 2GB, and multiple ports can be opened when more memory is needed.
(3) Memcached is a multi-threaded, non-blocking io multiplexing network model, and Redis is a single thread.
(4) The maximum key length is 250 bytes.
Thirty, the way to share Session
1) Session sharing based on NFS. NFS (Network File System) was first developed by Sun to solve directory sharing between Unix network hosts. Just mount the shared directory server to the local session directory of other servers.
2) Session sharing based on database.
3) Session sharing based on Cookie. The principle is to encrypt and serialize the session information of all users on the entire site in the form of cookies, and plant them under the root domain name (for example: .host.com). When using a browser to visit all secondary domain sites under the root domain name, The characteristics of all Cookie content corresponding to the domain name will be transferred, so as to realize the shared access of the user's Cookie Session among multiple services.
4) Session sharing based on cache (Memcache). Memcache is a memory sharing system based on Libevent multi-channel asynchronous I/O technology. The simple key + value data storage mode makes the code logic small and efficient, so it has an absolute advantage in concurrent processing capabilities. It can currently reach an average of 2000/s. Query, and the server CPU consumption is still less than 10%.
31. How to solve the memcache or redis avalanche?
Cause: Generally, in a website, the mysql database handles fewer requests (20%), the load is 80%, and the caching technology handles most of the requests (80%)
If memcache or redis hangs, all requests will be processed in mysql, and the database will go down if its processing capacity is insufficient. At this time, even restarting the cache and mysql will not help, because after the cache is restarted, the data has been lost, data requests will still go to mysql, and mysql will still die (infinite loop)
1: Start the cache first, and then start the database. (However, no external services are provided at this time)
2: Write commonly used keys into the cache through a PHP script
3: Open external services [hot data has been cached, and requests will be cached to reduce the pressure on mysql]
32. How does Redis persist?
1.Aof(append only file)
When redis executes a command, it will append the command we executed in the form of a log. Security is high, but it affects performance.
Persistence in accordance with established rules
save 900 1 (1 redis operation in 900s will do a persistence)
save 300 10 (10 redis operations within 300s will be persisted once)
save 60 10000 (10000 redis operations in 60s will be persisted once)
However, there may be data loss. For example, if we do a persistence at 12:00, it will be persisted again at 12:15 if normal. If the cache is dead at 12:14, then 14 minutes of data will be lost. Not very safe, but performance is much better than AOF
Thirty-three, the way of communication between processes in the Linux system.
Pipes are divided into named pipes and unnamed pipes
An anonymous pipe is a half-duplex communication method. Data can only flow in one direction, and it can only be used between processes that have kinship. The kinship of processes generally refers to the parent-child relationship. Ignoring pipes are generally used for communication between two different processes. When a process creates a pipe and calls fork to create its own child process, the parent process closes the reading pipe end, and the child process closes the writing pipe end, which provides a way for data flow between the two processes.
The well-known pipe is also a half-duplex communication method, but it allows communication between unrelated processes.
The message queue is a linked list of messages, stored in the kernel and identified by the message queue identifier. The message queue overcomes the lack of signal transmission information, the pipeline can only carry unformatted byte streams and the buffer size is limited. The message queue is UNIX A mechanism for sharing resources between different processes. UNIX allows different processes to send formatted data streams to any process in the form of a message queue. Processes that have operating rights on the message queue can use msget to complete the message queue Operational control. By using message types, the process can read information in any order or prioritize messages.
Signal is a more complicated communication method used to notify the receiving process that an event has occurred.
A semaphore is a counter that can be used to control the access of multiple threads to shared resources. It is not used to exchange large amounts of data, but to synchronize between multiple threads. It is often used as a locking mechanism to prevent a process from being When accessing a resource, other processes also access the resource. Therefore, it is mainly used as a means of synchronization between processes and between different threads in the same process.
Shared memory is to map a section of memory that can be accessed by other processes. This shared memory is created by one process, but it can be accessed by multiple processes. Shared memory is the fastest IPC (inter-process communication) method, it is for other processes The inter-communication method has low operating efficiency and is specially designed. It is often used in conjunction with other communication mechanisms, such as semaphores, to achieve synchronization and communication between processes.
It can be used for different process communication files, mutual exclusions, etc., but I saw in the swoole source code that process communication is done in this way through eventfd
34. Summary of massive data processing
1. Mass log data, extract the IP with the most visits to Baidu on a certain day.
Algorithm idea: Divide and conquer +Hash
The IP address has at most 2^32=4G values, so it cannot be completely loaded into the memory for processing
Consider adopting the idea of "divide and conquer". According to the Hash(IP)%1024 value of the IP address, the massive IP logs are stored in 1024 small files. In this way, each small file contains at most 4MB IP addresses
For each small file, you can construct a Hash map with IP as the key and the number of occurrences as the value, and record the current IP address with the most occurrences.
You can get the IP with the most occurrences in 1024 small files, and then get the IP with the most occurrences in general according to the conventional sorting algorithm
Thirty-five. Two mysql servers, one of which is down, how to make the business end switch unintentionally, and ensure that the data of the platform server is consistent under normal circumstances
If it is not the core business, stop writing first, pull up the standby machine, check the logs of the two machines, perform data compensation, and start writing.
If it is the core business, all write operations are now on the normal state machine. Pull up the standby machine of this machine and use it as the main machine.
What should I do if the data of the standby machine is inconsistent?
You have to be brave enough to go back, how many write operations per second you do. According to the million-level table, with a write efficiency of 1000 per second, the normal design is to distribute 500 on each of 2 machines. At this level of data synchronization, the probability of discrepancies is negligible. If there is a problem with one, the other can also resist.
Thirty-six. How does redis synchronize, the method of synchronization, what to do with synchronization rollback, and what to do if data is abnormal
The simple principle of redis cluster master-slave synchronization
The replication function of Redis is based on the persistence strategy of memory snapshots, which means that no matter what your persistence strategy chooses, as long as the replication function of Redis is used, there will be memory snapshots.
When the Slave is started and connected to the Master, it will actively send a SYNC command (1. the Master will start a background process and save the data snapshot to a file [rdb file] The Master will send a SYNC command to the Slave
Ping command to determine the survival status of the Slave. When alive, the Master will send the data file to the Slave and send all write commands to the Slave). Slave first saves the data file locally and then loads the data into the memory.
After the first connection or failure, the reconnection will first determine the survival status of the Slave and then synchronize all data, and then only synchronize the write operation of the Master (send the command to the Slave)
When the Master synchronizes data, if the amount of data is large and the Master itself will only enable a background process to synchronize multiple slaves, the Master will be overwhelmed and the slave recovery time will be very slow!
The advantages of redis master-slave replication:
(1) In a Redis cluster, the master is responsible for write requests, and the slave is responsible for read requests. This way, on the one hand, the pressure on the master server is greatly reduced by distributing read requests to other machines. On the other hand, the slave focuses on providing
The read service thus improves the response and reading speed.
(2) In a Redis cluster, if the master goes down, the slave can intervene and replace the position of the master. Therefore, the entire Redis service will not fail to provide services, which makes the entire Redis service sufficiently secure.
(3) Increasing the level of Slave machines can improve performance
37. How to solve cross-domain
Add response headers, allowing cross-domain
Way of agency
Thirty-eight, write the following output
Q: "aa" == 1, "aa" == 0, 1 == "1", 1==="1", "12asdsad" + 1, "asdjkfgj12"+1 A: false, true, true, false, 13, 1
String == 0 in php is always established
In php, the string and the number are added together. If the string starts with a number, it is equal to the number at the beginning of the string (the string starts at the first position and ends at the first non-number and .) + number
39. What is a service container, inversion of control (IoC), dependency injection (DI)
The service container is a tool used to manage class dependencies and run dependency injection. In the Laravel framework, the service container is used to implement inversion of control and dependency injection.
Inversion of Control (IoC) means to transfer the control of creating objects. Previously, the initiative and timing of creating objects was controlled by oneself, but now this power is transferred to a third party, which is the container in Laravel.
Dependency injection (DI) is to help the container to dynamically provide resources for objects in the runtime.
Forty, Composer automatic loading principle
The core idea of composer loading is to load the correspondence between the class and the path into the memory when the entry file (autoload.php) is referenced through the configuration file of the composer, and finally register the specific loaded implementation in the spl_autoload_register function. Finally, the required file Include in.
Forty-one. The main process of a request to PHP and Nginx. A complete description of the entire network request process and principle.
1) The FastCGI process manager php-fpm initializes itself, starts the main process php-fpm and starts the start_servers CGI child processes. The main process php-fpm mainly manages fastcgi child processes and monitors port 9000. The fastcgi child process waits for a connection from the Web Server.
2) When the client request arrives at the Web Server Nginx, Nginx passes all the files with the suffix php to 127.0.0.1:9000 through the location instruction, that is, Nginx uses the location instruction to send all files with the suffix php All files are handed over to 127.0.0.1:9000 for processing.
3) FastCGI process manager PHP-FPM selects and connects to a child process CGI interpreter. The Web server sends the CGI environment variables and standard input to the FastCGI child process.
4) After the FastCGI sub-process completes the processing, it returns the standard output and error information from the same connection to the Web Server. When the FastCGI child process closes the connection, the request is processed.
5) The FastCGI child process then waits and processes the next connection from the FastCGI process manager (running in the WebServer).
Forty-two, the magic method of PHP
__set()//When assigning a value to an inaccessible attribute, __set() will be called
__get()//When reading the value of an inaccessible attribute, __get() will be called
__isset()//When isset() or empty() is called on an inaccessible property, __isset() will be called
__unset()//When unset() is called on an inaccessible property, __unset() will be called
__call()//When calling an inaccessible method in the object, __call() will be called
__callStatic()//When an inaccessible method is called in a static context, __callStatic will be called
__construct()//The class of the constructor will call this method every time a new object is created, so it is very suitable to do some initialization work before using the object.
__destruct()//The destructor will be executed when all references to an object are deleted or when the object is explicitly destroyed.
__sleep()//The serialize() function checks whether there is a magic method __sleep() in the class. If it exists, the method will be called first, and then the serialization operation will be performed. This function can be used to clean up the object and return an array containing the names of all variables in the object that should be serialized. If the method does not return anything, NULL is serialized and an E_NOTICE level error is generated.
__wakeup()//The unserialize() function will check whether there is a __wakeup() method, if it exists, it will call the method first, and then perform the deserialization operation. __wakeup() is often used in deserialization operations, such as re-establishing a database connection or performing other initialization operations.
Forty-three, the difference between character encoding UTF8, GBK, and GB2312.
utf8 is the international code. Good versatility.
gbk is a domestic code. The general type is worse than utf8, but it occupies a smaller database than utf8.
gb2312 is a Chinese national standard for simplified Chinese character sets, containing 6763 Chinese characters in total.
Forty-four, what is the default sorting method of MySQL
MyIsam storage engine: In the absence of any delete or modify operation, execute select without order by, then it will be sorted in the order of insertion.
InnDB storage engine: In the same situation, select without order by will be sorted according to the primary key, from small to large.
Forty-five, OSI seven-layer network model
Physical layer: establish, maintain, and disconnect physical connections
Data link layer: establish logical links, perform hardware address addressing, error checking and other functions (SDLC, HDLC, PPP, STP)
Network layer: Perform logical address addressing to realize path selection between different networks (IP, IPX, OSPF)
Transport layer: Define the protocol port number for data transmission, as well as the process and error checking (TCP, UDP) data packets enter the network transport layer once they leave the network card
Session layer: establish, manage, and terminate sessions
Presentation layer: data presentation, security, compression
Application layer: an interface between network services and end users; protocols include: HTTP, FTP, TFTP, SMTP, DNS, TELNET, HTTPS, POP3, DHCP
Pay attention, don't get lost
Alright, everyone, the above is the whole content of this article. The people who can see here are talents . As I said before, there are many technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can
Click to enter the secret code: PHP+ Platform
For more learning content, please visit the [Comparative Standard Factory] Catalogue of Excellent PHP Architect Tutorials, as long as you can read it to ensure that the salary will rise one step (continuous update)
The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Multiple knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group