php 笔试题
第一部分
- 程序如下,写出执行结果 - $count = 3; function get_count() { static $count = 0; return $count++; } echo $count; //3 ++$count; echo get_count(); //0 echo get_count(); //1
- 有文件dir/upload.img.jpg,使用2种以上方法获取拓展名 - // pathinfo pathinfo($dir)['extension']; array ( 'dirname' => 'dir', 'basename' => 'upload.img.jpg', 'extension' => 'jpg', 'filename' => 'upload.img', ) // 截取字符串 substr($dir, strrpos($dir, '.')); // 分成数组 $arr = explode('.', $dir); echo array_pop($arr); // 从右侧搜素 echo strrchr($dir, '.');
- 获取ip地址 - $_SERVER['REMOTE_ADDR'];
- MyISAM和InnoDB的主要区别 
 https://blog.csdn.net/qq_35642036/article/details/82820178- 1.InnoDB支持事务,MyISAM不支持,对于InnoDB每一条SQL语言都默认封装成事务,自动提交,这样会影响速度,所以最好把多条SQL语言放在begin和commit之间,组成一个事务 2.InnoDB支持表、行(默认)级锁,而MyISAM支持表级锁 3.InnoDB支持外键,而MyISAM不支持 4.InnoDB是聚集索引,MyISAM是非聚集索引.也就是说:InnoDB的B+树主键索引的叶子节点就是数据文件,辅助索引的叶子节点是主键的值;而MyISAM的B+树主键索引和辅助索引的叶子节点都是数据文件的地址指针。
- php如何读取文件内容 - // 方法1 file_get_contents // 方法2 $file_path = "test.txt"; if (file_exists($file_path)) { $fp = fopen($file_path, "r"); $str = fread($fp, filesize($file_path)); //指定读取大小,这里把整个文件内容读取出来 echo $str; fclose($fp); } if (file_exists($file_path)) { $fp = fopen($file_path, "r"); $str = ""; while (!feof($fp)) { $str .= fgets($fp); //逐行读取。如果fgets不写length参数,默认是读取1k。 } echo $str; fclose($fp); }
- 常见php魔术方法 
 https://www.php.net/manual/zh/language.oop5.magic.php- __construct __destruct __call __callStatic __get __set
- 现有字符串'测,试,,文,,,本,',去掉字符串中多余的逗号,结果为'测,试,文,本' - function solution($str) { $res = ''; for ($i = 0; $i < mb_strlen($str); $i++) { $now = mb_substr($str, $i, 1); $next = mb_substr($str, $i + 1, 1); if (($now == ',' && $next == ',') || ($now == ',' && !$next)) { continue; } $res .= $now; } return $res; }
- 快速排序 
 https://blog.iguojin.com/php/5166.html- 第二部分
- 字符串比较 - if (date('H:i') > '9:30') { echo '时间外'; } else { echo '时间内'; } // 因为这种普通字符串比较时,会逐个字符去比较,H时候第一位是0或者1<=9,所以不会走到时间内
- 浮点型算不准 
 http://www.laruence.com/2013/03/26/2884.html- $a = 3.57; $b = 4.58; $res1 = $a - 0.47; $res2 = $b - 1.48; var_export($res1 == $res2); // false // 浮点型内部使用科学记数法,有可能产生无限循环 // 不能用普通等号比较浮点型相等
- 给定一个二维数组,每一行是递增的,每一列也是递增的,输入一个整数,判断是否在这个数组中 - function solution($arr, $find) { foreach ($arr as $k => $row) { for ($i = count($arr[0]) - 1; $i >= 0; $i--) { if ($row[$i] < $find) { break; } if ($row[$i] == $find) { return true; } } } return false; } $arr = [ [1, 2, 8, 9], [2, 4, 9, 12], [4, 7, 10, 13], [6, 8, 11, 15], ]; // O(n^2) var_export(solution($arr, 9));
- 把数组若干元素搬到数组末尾,称为数组旋转,给定一个 - 递增数组的旋转数组,输出该数组最小元素,并给出复杂度- function solution($arr) { for ($i = 0; $i < count($arr) - 1; $i++) { if ($arr[$i] > $arr[$i + 1]) { return $arr[$i + 1]; } } } $arr = [4, 5, 6, 7, 8, 10, 1, 2, 3]; // O(n) var_export(solution($arr));
- 输入一个字符串,输出字符串所有组合 
- php中static用法,调用子类新方法 - class A { public static function who() { echo __CLASS__; } public static function test() { static::who(); } } class B extends A { public static function who() { echo __CLASS__; } } // B B::test();
- self,parent用法 - class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__ . PHP_EOL; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } public static function who() { echo __CLASS__ . PHP_EOL; } } class C extends B { public static function who() { echo __CLASS__ . PHP_EOL; } } // A // C // C // A::foo()显式调用父类方法没有继承关系,有继承关系时候parent和self调用,static调用的是最终的子类 C::test();
最后更新于 2019-10-22 00:18:51 并被添加「PHP 笔试题」标签,已有 1418 位童鞋阅读过。
此处评论已关闭