The most recent PHP interview question record, the office has already gotten!

1. Explain which levels are specific

Specifically, system, const, range, index, all

2. MySQL optimization

  • Avoid full-table queries, first consider creating indexes on the columns involved in where and order by

  • Should try to avoid the null value judgment of the field in the where clause, otherwise it will cause the engine to give up using the index and perform a full table scan (you can set the default value of the field to 0)

  • Try to avoid using it in the where clause! = Or <> operator, otherwise the engine will give up using the index and perform a full table scan

  • Should try to avoid using or in the where clause to join conditions, otherwise it will cause the engine to give up using the index and perform a full table scan, such as: select id from t where num=10 or num=20 You can query like this: select id from t where num=10 union all select id from t where num=20

  • n and not in should also be used with caution, otherwise it will cause a full table scan, such as: select id from t where num in (1,2,3) For continuous values, if you can use between, don’t use in: select id from t where num between 1 and 3

  • The following query will also cause a full table scan: select id from t where name like'% Li%' To improve efficiency, you can consider full-text search.

  • Should try to avoid performing expression operations on the field in the where clause, which will cause the engine to abandon the use of the index and perform a full table scan

  • Do not perform functions, arithmetic operations or other expression operations on the left side of the "=" in the where clause

  • When using an index field as a condition, if the index is a compound index, then the first field in the index must be used as a condition to ensure that the system uses the index, otherwise the index will not be used, and should be used as much as possible The order of the fields is consistent with the index order.

  • In many cases, using exists instead of in is a good choice: select num from a where num in (select num from b) is
    replaced with the following statement:
    select num from a where exists(select 1 from b where num=a.num)

  • Indexes are not as many as possible. While indexing can improve the efficiency of the corresponding select, it also reduces the efficiency of insert and update, because the index may be rebuilt during insert or update, so how to build an index needs to be carefully considered, depending on the specifics. It depends on the situation. The number of indexes of a table should not exceed 6, if there are too many, you should consider whether it is necessary to build indexes on columns that are not frequently used.

  • Do not use select from t anywhere, replace "" with a specific field list, and do not return any unused fields.

  • Try to avoid large transaction operations and improve system concurrency.

  • Sub-database

  • Classification table sub-database

3. What types of redis support

Supports 5 types: string, hash, linked list, ordered, and unordered. In the latest redis 5.0, the stream type is newly added to support queues very efficiently.

4. The difference between PHP7 and php5

  • foreach no longer changes the internal array pointer

  • Scalar type declaration. In the old version, the parameter declaration of the function can only be (Array $arr), (CLassName $obj), etc. Basic types such as string (string), integer (int), floating-point number (float), and Boolean Value (bool) etc. cannot be declared

  • Added support for return type declaration

declare(strict_types=1);
function add(int $a, int $b){
    
    
return $a+$b;
}

echo add(1,2);
echo add(1.5, 2.6);
  • null coalescing operator

There are a large number of cases where ternary expressions and isset () are used at the same time in the project, and the syntactic sugar of the null merge operator (??) is added. If the variable exists and the value is not NULL, it will return its own value, otherwise it will return its second operand.

旧版:isset ($_GET [‘id’]) ? $_GET [id] : err;

New version: $_GET ['id'] ??'err';

  • Define a constant array by define ()
PHP 5.6 中仅能通过 const 定义常量数组,PHP 7 可以通过 define() 来定义。
<?php
define('ANIMALS',
['dog', 'cat', 'bird']);
echo ANIMALS[1];
// outputs "cat"

const name = ['1','2'];
echo name[1];
  • Now supports the instantiation of an anonymous class through new class, which can be used to replace some complete class definitions that are "burned after use"

  • preg_replace_callback_array()

​ Added a new function preg_replace_callback_array (), using this function can make the code more elegant when using the preg_replace_callback () function. Before PHP7, the callback function would call every regular expression, and the callback function was tainted in some branches.

5. Pointer problem

<?php
$array = [1,2,3];
foreach ($array as $k=>&$v){
    
    
    $v++;
}

var_dump($array);
foreach ($array as $k=>$v){
    
    
    echo $v;
}

233

6、array_column()

Returns the value of a single column in the input array.

array_column(array,column_key,index_key);

<?php
// 可能从数据库中返回数组
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Ben',
    'last_name' => 'Smith',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Joe',
    'last_name' => 'Doe',
  )
);

$last_names = array_column($a, 'last_name', 'id');
print_r($last_names);
?>

result

Array
(
    [5698] => Griffin
    [4767] => Smith
    [3809] => Doe
)

7. Take out the first five call records

select name,count(*)  as num  from order
where date_sub(curdate(), INTERVAL 10 DAY) <= date(`created_at`)
group by name 
order by num desc 
limit 10;

8. PHP extension installation

    //下载libevent扩展文件压缩包(在当前系统哪个目录下载随意)
~# wget http://pecl.php.net/get/libevent-0.1.0.tgz
    //解压文件
~# tar -zxvf libevent-0.1.0.tgz
    //进入源码目录
~# cd libevent-0.1.0/
    //运行phpize命令,写全phpize的路径
~# /usr/local/php/bin/phpize
    //运行configure命令,配置时 要将php-config的路径附上
~# ./configure --with-php-config=/usr/local/php/bin/php-config
    //运行make命令
~# make
    //测试编译安装
~# make test
    //正式编译安装
~# sudo make install
    //修改php.ini,结尾加入:extension=libevent.so
    //重启对应的php-fpm

9. The relationship between session and cookie

前提是服务端开启了session
1. 第一次访问页面时, 服务端生成一个不重复的sessionid(当前会话id)以及命名为sess_xxx的session文件
该session文件保存在php.ini文件中指定的目录, xxx是sessionid, sessionid可以通过session_id()函数来获取

2. 服务端向客户端返回响应, 其中有响应头Set-Cookie:PHPSESSID=xxx

3. 客户端收到Set-Cookie响应头, 将sessionid写入cookie, cookie的key为PHPSESSID, value为sessionid
比如PHPSESSID=jlis2mcmv6d5hejkemom77ibm3

4. 当第二次访问页面时, 客户端会把cookie放在请求头(Request Header), 服务端识别PHPSESSID这个cookie
然后根据这个cookie获取当前会话ID(sessionid), 从而找到对应的session文件, 再从session文件中读取信息

10. jsonp principle

11. Ajax cross-domain

12. The principle of master-slave replication

13, operator precedence problem

<?php
$tmp = 0 == 'a' ? 1: 2;
echo $tmp;
?>

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of 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」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a 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. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/109123496