PHP Interview Questions(1)

面试时间:2018 年 8 月 31 日,星期五,下午两点。


PHP 基础

1. 写个匹配日期格式为 “2018-01-01 08:00:00 AM” 的正则表达式

preg_match("/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\sAM|PM$/", "2018-01-01 08:00:00 AM");

2. 类似 _constrcut() 的常见的魔术方法还有哪些(3 – 5 个即可)

http://php.net/manual/zh/language.oop5.magic.php

3. 简述几个 http 状态码 40X 、50X 的区别

40X 客户端请求错误
50X 服务器错误

4. array + array 与 array_merge() 的区别

$arr1 = [1, 2, 3, 'color' => 'red'];
$arr2 = [4, 5, 6, 7, 8, 'color' => 'green'];

$arr1 + $arr2 = [1, 2, 3, 'color' => 'red', 7, 8];

array_merge($arr1, $arr2) = [1, 2, 3, 'color' => 'green', 4, 5, 6, 7, 8];

5. 简述 include 和 require 的区别

https://stackoverflow.com/questions/2418473/difference-between-require-include-require-once-and-include-once

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

存储

1. MySQL 存储引擎 MyISAM 与 InnoDB 区别(不少于 2 点)

                                                 MyISAM   InnoDB
----------------------------------------------------------------
Required full-text search                        Yes      5.6.4
----------------------------------------------------------------
Require transactions                                      Yes
----------------------------------------------------------------
Frequent select queries                          Yes      
----------------------------------------------------------------
Frequent insert, update, delete                           Yes
----------------------------------------------------------------
Row locking (multi processing on single table)            Yes
----------------------------------------------------------------
Relational base design                                    Yes

https://stackoverflow.com/questions/20148/myisam-versus-innodb

2. MySQL ABC 联合索引,以下哪些可以走到索引

A: AB   B: AC   C: BC   D: CA   E: CA

AB

3. 简单介绍 memcache 和 redis 的区别(不少于 2 点)

redis 支持持久化,更多的数据类型。

https://stackoverflow.com/questions/10558465/memcached-vs-redis

代码

1. 写个函数对数组 [{“name”:”sam”, “age”:22}, {“name”:”jim”, “age”:20}, {“name”:”paul”, “age”:26}, …] 按年龄倒叙排序

$array = [
    ["name" => "sam", "age" => 22],
    ["name" => "jim", "age" => 20],
    ["name" => "paul", "age" => 26]
];

usort($array, function($a, $b) {
    return $b['age'] - $a['age'];
});

2. 假设有这样一个二维数组

[
["id"=>1, "pid"=>0, "name"=>"a"],
["id"=>2, "pid"=>1, "name"=>"b"],
["id"=>3, "pid"=>1, "name"=>"c"],
["id"=>4, "pid"=>2, "name"=>"d"],
...
]

表示这样的一棵树

a
|  \
b   c
|
d

请写个函数将二维数组转换成这样的 json 输出

{
"id": 1, "name": "a",
"children": [
    {
        "id": 2, "name": "b",
        "children": [
            {"id": 4, "name": "d"}
        ]
    },
    {"id": 3, "name": "c"}
]
}
$array = [
    ["id"=>1, "pid"=>0, "name"=>"a"],
    ["id"=>2, "pid"=>1, "name"=>"b"],
    ["id"=>3, "pid"=>1, "name"=>"c"],
    ["id"=>4, "pid"=>2, "name"=>"d"]
];

class TreeNode
{
    public $id;
    public $name;
}

class Tree {
    public static function toJson($array) {
        $nodes = [];
        foreach ($array as $k => $v) {
            $node = new TreeNode();
            $node->id = $v['id'];
            $node->name = $v['name'];
            $nodes[$v['id']] = $node;
            if ($v['pid'] === 0) {
                $head = $nodes[$v['id']];
            } else {
                $nodes[$v['pid']]->children[] = $node;
            }
        }
        return json_encode($head);
    }
}

echo Tree::toJson($array);

面试部分

Linux

  • 查看进程
  • 查看端口占用
  • 统计字符串出现次数

Nginx

MySQL

Redis

PHP

JQuery

算法

 456 total views,  1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *