Share some important php knowledge points in php

小云云
Release: 2023-03-21 16:20:01
Original
5345 people have browsed it

1. Brief description of autoload in php

When using a class in PHP, we must load it before use, whether it is through require or include, but there are two ways This question affects our decision to load. The first is that I don't know where this class file is stored, and the other is that I don't know when I need to use this file. Especially when there are a lot of project files, it is impossible to write a long list of requirements at the beginning of each file.
Autoload loading mechanism, when a class is instantiated through new, PHP will load the corresponding file through the defined autoload function. If this class file uses extends or implements and needs to use other class files, PHP will re- Run autoload to search and load class files. If two requests for the same class file occur, an error will be reported.

 2. What are the advantages and disadvantages of static variables?

Characteristics of static local variables: 1. It will not change as the function is called and exits. However, although the variable continues to exist, it cannot be used. If the function that defines it is called again, it can continue to be used, and the value left after the previous call is saved. 2. Static local variables will only be initialized once. 3. Static properties can only be initialized to a character value or a constant, and expressions cannot be used. Even if the local static variable is defined without an initial value, the system will automatically assign an initial value of 0 (for numeric variables) or a null character (for character variables); the initial value of the static variable is 0. 4. When a function is called multiple times and Consider using static local variables when you need to preserve the value of some variables between calls. Although global variables can also be used to achieve the above purpose, global variables sometimes cause unexpected side effects, so it is still better to use local static variables.

3. What is the difference between strtr and str_replace? They are used in In what scenario?

str_replace() function replaces some characters in the string with other characters (case sensitive)

strtr() function converts specific characters in the string.

5.6 version str_replace is 10+ times more efficient than strtr, 7.0 version is basically the same efficiency, but 5.6 str_replace is 3 times more efficient than 7.0

4. Magic method

__construct (): The default constructor method of the class. If __construct() and a method with the same name as the class appear together, __construct() will be called by default instead of the method with the same name.

__call(): When calling a method that does not exist or is inaccessible, the __call($name, $arguments) method will be called.

__toString(): Will be called directly when printing the object. For example, echo $object;

__clone(): called directly when the object is copied.

__isset(): When isset() or empty() is used for non-existent or inaccessible properties, __isset() will be called;

__destruct(): Class destructor , executed when all references to the object are removed, or the object is explicitly destroyed.

5. As shown below, what result will be output?

foreach ($array as $key => $item) { $array[$key + 1] = $item + 2; echo "$item"; } print_r($array); 结果示例: $array = [3,6,7,8]; 3678 //echo 输出数组内元素的值Array( [0] => 3 //$key 保持不变 [1] => 5 //每次的$eky + 1,对应的值加2, [2] => 8 [3] => 9 [4] => 10)
Copy after login

laravel

The advantages and disadvantages of laravel compared with other frameworks

laravel

Advantages: The number of users in the world is the largest, the documentation is complete, and the framework structure is clearly organized , a large number of third-party extension packages are available for reference, which is suitable for collaborative development of large websites. The artisan development tools provided have high development efficiency. Composer extension automatically loads, middleware

Disadvantages: slightly complex, slower to get started than ordinary frameworks; a large number of third-party packages are referenced, but in some scenarios we only use some methods in the class, and the code seems a bit redundant

ThinkPHP

Advantages: It is a fast and simple lightweight PHP development framework based on MVC and object-oriented. It is released under the Apache2 open source agreement, adhering to simple and practical design principles, while maintaining excellent While having excellent performance and minimal code, it pays special attention to development experience and ease of use, and has many original functions and features, providing strong support for WEB application development. Simple, clear, convenient and quick to get started

Disadvantages: Lack of object-oriented design, version 5 is basically object-oriented, and there are few auxiliary tools related to the framework community

Mysql

1. In Mysql, the difference between int(10) and int(11)

BIT[M] bit field type, M represents the number of digits in each value, ranging from 1 to 64, if M is ignored, the default is 1

TINYINT [(M)] [UNSIGNED] [ZEROFILL] M defaults to 4. Very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

SMALLINT[(M)] [UNSIGNED] [ZEROFILL] M defaults to 6. small integer. The signed range is -32768 to 32767. The unsigned range is 0 to 65535.

MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL] M defaults to 9. Medium sized integer. The signed range is -8388608 to 8388607. The unsigned range is 0 to 16777215.

INT[(M)] [UNSIGNED] [ZEROFILL] M defaults to 11. A regular-sized integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.

BIGINT[(M)] [UNSIGNED] [ZEROFILL] M defaults to 20. Large integer. The signed range is -9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615.

Note: M here does not represent the specific length stored in the database. In the past, it was always mistakenly believed that int(3) can only store numbers of 3 lengths, and int(11) will store 11 lengths. The numbers are dead wrong. In fact, when we choose to use the type of int, whether it is int(3) or int(11), it stores a length of 4 bytes in the database. When using int(3), if you enter is 10, it will give you the storage bit 010 by default, which means that 3 represents a default length. When you are less than 3 digits, it will help you incomplete. When you exceed 3 digits, there will be no impact.
**int(M) M indicates the maximum display width. The maximum effective display width is 255. The optional display width specification is used to fill the width from the left when the display width is smaller than the specified column width. Display width does not limit the range of values that can be held within the column, nor does it limit the display of values that exceed the specified width of the column.

2. Index left principle:

like, when matching strings, do not start with a wildcard character, the left side must be fixed, then the field index will work

Compound index , when the field on the left is fixed, the index on the right is only valid when the index matches. Because the compound index keyword sorting is based on the left field, if the left field is the same, it is sorted according to the right field.

3. Advantages and disadvantages of index creation:

Advantages:

Creating an index can greatly improve the performance of the system

Through unique indexes, the database can be guaranteed The uniqueness of each row of data in the table

Greatly speeds up retrieval

Accelerates the connection between tables

When using grouping and sorting clauses for data retrieval, reduce Time for grouping and sorting in queries

By using indexes, you can use optimization hiders during the query process to improve system performance

Disadvantages:

Create and maintain indexes It takes time and increases as the amount of data increases

The index occupies physical space

When adding, deleting, or modifying data in the table, the index needs to be dynamically maintained, which reduces the data maintenance speed

4. Please describe how data is synchronized between the mysql master and slave servers. What kind of SQL will cause the master and slave to fail to synchronize correctly?

Network delay

Since mysql master-slave replication is an asynchronous replication based on binlog, binlog files are transmitted through the network. Of course, network delay is the vast majority of reasons for master-slave synchronization. Especially for cross-computer room data synchronization, the probability of this happening is very high, so separate reading and writing, and pay attention to the early design from the business layer.

The loads of the master and slave machines are inconsistent

Because mysql master-slave replication starts an io thread on the master database, and starts 1 sql thread and 1 io thread from above, among which The load of any machine is very high and it is too busy. As a result, any one of the threads has insufficient resources, and master-slave inconsistency will occur.

max_allowed_packet settings are inconsistent

The max_allowed_packet set on the master database is larger than the slave database. When a large SQL statement can be executed on the master database, the max_allowed_packet set on the slave database is too small and cannot be executed. , resulting in master-slave inconsistency.

The master-slave inconsistency is caused by the inconsistency between the key value starting from the key auto-increment key and the auto-increment step setting.

When mysql is abnormally down, if sync_binlog=1 or innodb_flush_log_at_trx_commit=1 is not set, it is very likely that the binlog or relaylog file will be damaged, resulting in master-slave inconsistency.

The master-slave synchronization is caused by a bug in mysql itself.

The versions are inconsistent, especially when the higher version is the master and the lower version is the slave, the function supported on the master database is not supported on the slave database

5. There is an order table , as follows, find the two users with the most purchases

order_id

user_id

goods

100000 100 Apple

100001 100 Apple

100002 101 Oranges

100003 102 Apples

100004 102 Bananas

sql:

SELECT order_id,user_id,COUNT(order_id) AS count FROM order GROUP BY user_id ORDER BY count DESC limit 2

Linux

How to find out that the web service is slow

top: Check system performance

Nginx: Add $request_time to the last field

List the pages whose PHP page request time exceeds 3 seconds, and count the number of occurrences, and display the first 100
cat access.log|awk '($ NF > 1 && $7~/\.php/){print $7}'|sort -n|uniq -c|sort -nr|head -100

Implemented in the code, write the time at the beginning and the end Writing time

Comprehensive

1. The difference between AES and RSA:

RSA is asymmetric encryption, public key encryption, private key decryption, and vice versa. Disadvantages: Slow running speed and difficult to implement in hardware. Common private key lengths are 512bit, 1024bit, 2048bit, and 4096bit. The longer the length, the more secure it is, but the slower the key generation is, and the more time-consuming encryption and decryption are.

AES symmetric encryption, the longest key is only 256 bits, the execution speed is fast, and it is easy to implement in hardware. Because it is symmetric encryption, the key needs to be known to both communicating parties before transmission.

AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个(如果数据块及密钥 长度不足时,会补齐)

总结:采用非对称加密算法管理对称算法的密钥,然后用对称加密算法加密数据,这样我们就集成了两类加密算法的优点,既实现了加密速度快的优点,又实现了安全方便管理密钥的优点。

RBAC:基于角色的访问控制

一个用户属于多个角色

角色拥有多个动作的权限

用户是否具有某些动作的权限

表:用户表、角色表、动作表,用户角色关联表、角色动作关联表

MongoDB

MongoDB数据类型:

String(字符串): mongodb中的字符串是UTF-8有效的。

Integer(整数): 存储数值。整数可以是32位或64位,具体取决于您的服务器。

* Boolean(布尔): 存储布尔(true/false)值。

Double(双精度): 存储浮点值。

Min/ Max keys(最小/最大键): 将值与最低和最高BSON元素进行比较。

Arrays(数组): 将数组或列表或多个值存储到一个键中。

Timestamp(时间戳): 存储时间戳。

Object(对象): 嵌入式文档。

Null (空值): 存储Null值。

Symbol(符号): 与字符串相同,用于具有特定符号类型的语言。

Date(日期): 以UNIX时间格式存储当前日期或时间。

Object ID(对象ID) : 存储文档ID。

Binary data(二进制数据): 存储二进制数据。

Code(代码): 将JavaScript代码存储到文档中。

Regular expression(正则表达式): 存储正则表达式

算法

1. 排序算法

* 快速排序

快速排序是十分常用的高效率的算法,其思想是:我先选一个标尺,用它把整个队列过一遍筛选,以保证其左边的元素都不大于它,其右边的元素都不小与它function quickSort($arr){ // 获取数组长度

$length = count($arr); // 判断长度是否需要继续二分比较 if($length <= 1){ return $arr; } // 定义基准元素 $base = $arr[0]; // 定义两个空数组,用于存放和基准元素的比较后的结果 $left = []; $right = []; // 遍历数组 for ($i=1; $i < $length; $i++) { // 和基准元素作比较 if ($arr[$i] > $base) { $right[] = $arr[$i]; }else { $left[] = $arr[$i]; } } // 然后递归分别处理left和right $left = quickSort($left); $right = quickSort($right); // 合并 return array_merge($left,[$base],$right); }
Copy after login

冒泡排序

思路:法如其名,就像冒泡一样,每次从数组中冒出一个最大的数
比如:2,4,1
第一次冒出4:2,1,4
第二次冒出2:1,2,4

function bubbleSort($arr){ // 获取数组长度 $length = count($arr); // 第一层循环控制冒泡轮次 for ($i=0; $i < $length-1; $i++) { // 内层循环控制从第0个键值和后一个键值比较,每次冒出一个最大的数 for ($k=0; $k < $length-$i; $k++) { if($arr[$k] > $arr[$k+1]){ $tmp = $arr[$k+1]; $arr[$k+1] = $arr[$k]; $arr[$k] = $tmp; } } } return $arr; }
Copy after login

选择排序

思路:每次选择一个相应的元素,然后将其放到指定的位置

function selectSort($arr){ // 实现思路 // 双重循环完成,外层控制轮数,当前的最小值,内层控制比较次数 // 获取长度 $length = count($arr); for ($i=0; $i < $length - 1; $i++) { // 假设最小值的位置 $p = $i; // 使用假设的最小值和其他值比较,找到当前的最小值 for ($j=$i+1; $j < $length; $j++) { // $arr[$p] 是已知的当前最小值 // 判断当前循环值和已知最小值的比较,当发下更小的值时记录下键,并进行下一次比较 if ($arr[$p] > $arr[$j]) { $p = $j; // 比假设的值更小 } } // 通过内部for循环找到了当前最小值的key,并保存在$p中 // 判断 日光当前$p 中的键和假设的最小值的键不一致增将其互换 if ($p != $i) { $tmp = $arr[$p]; $arr[$p] = $arr[$i]; $arr[$i] = $tmp; } } // 返回最终结果 return $arr; }
Copy after login

相关推荐:

PHP中的运算符及PHP知识基础

关于PHP知识点的详细介绍

对PHP知识的有关介绍介绍

The above is the detailed content of Share some important php knowledge points in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!