几道坑人的php面试题

来源:互联网 发布:漫步者煲机软件 编辑:程序博客网 时间:2024/05/16 01:29

前言

快开始校招了,我应聘的职位以php和c开发为主,最近也会从网上找几套php面试的题目,亲自做一遍并且记录一下

基础题

1.回答
<?php$str1 = null;$str2 = false;echo $str1 == $str2 ? '相等' : '不相等';

相等.

<?php$str1 = '';$str2 = 0;echo $str1 == $str2 ? '相等' : '不相等';

相等
 
<?php$str1 = 0;$str2 = '0';echo $str1 === $str2 ? '相等' : '不相等';

不相等

解释:
原因是在PHP中变量是以C语言的结构体来存储的,空字符串和null,false都是以值为0存储的,其中这个结构体有个zend_uchar type;这样的成员变量,他是用来保存变量的类型的,而空字符串的类型是string,NULL的类型是NULL,false是boolean

2.写出如下程序的输出结果

<?php$a1 = null;$a2 = false;$a3 = 0;$a4 = '';$a5 = '0';$a6 = 'null';$a7 = array();$a8 = array(array());echo empty($a1) ? 'true ' : 'false ';echo empty($a2) ? 'true ' : 'false ';echo empty($a3) ? 'true ' : 'false ';echo empty($a4) ? 'true ' : 'false ';echo empty($a5) ? 'true ' : 'false ';echo empty($a6) ? 'true ' : 'false ';echo empty($a7) ? 'true ' : 'false ';echo empty($a8) ? 'true ' : 'false ';?>

true true true true true false true false

解释:empty的判断更加严格


3.写出如下程序的输出结果

<?php$test = 'aaaaaa';$abc = &amp; $test;unset($abc);echo $abc;?>
aaaaaa

解释:引用传递,’aaaaaa’占用内存的引用数不为0,因此内存没有被释放,输出为aaaaaa


4.写出如下程序的输出结果

<?php$count = 5;function get_count(){    static $count = 0;    return $count ++;}echo $count;++ $count;echo get_count();echo get_count();?>

501

解释:基本的变量作用域和静态变量的特性



5.写出如下程序的输出结果

<?php$GLOBALS['var1'] = 5;$var2 = 1;function get_value(){    global $var2;    $var1 = 0;    return $var2 ++;}get_value();echo $var1;echo $var2;

52

解释: global var2使var2使用的是全局变量
var1使globalGLOBALS[‘var1’]来声明为全局变量,因此为局部变量,对局部变量的修改不会影响到全局变量


6.写出如下程序的输出结果

<?phpfunction get_arr($arr){    unset($arr[0]);}$arr1 = array(1, 2);$arr2 = array(1, 2);get_arr(&$arr1);get_arr($arr2);echo count($arr1);echo count($arr2);

12

解释:区分值传递和引用传递


7.使用5种以上的方式获取一个文件的扩展名

要求: dir/upload.image.jpg, 找出.jpg或者jpg

<?php/** * 五种方式获取指定路径的文件扩展名 */$str = "dir/upload.image.jpg";function one ($str){    $arr = explode('.', $str);    $count = count($arr);    return $arr[$count - 1];}function two ($str){    $len = strlen($str);    for ($i = $len - 1, $name = ''; $str[$i] != '.'; $i --) {        $name .= $str[$i];    }    $name = strrev($name);    return $name;}function three($str){    $path = pathinfo($str);    return $path['extension'];} function four($str){    $arr = explode('.', $str);    return array_pop($arr);}function five($str){    $start = strrpos($str, '.');    return substr($str, $start + 1);}echo one($str);echo "<br>";echo two($str);echo "<br>";echo three($str);echo "<br>";echo four($str);echo "<br>";echo five($str);echo "<br>";


算法题

1.使用PHP描述冒泡排序和快速排序,对象可以是一个数组

<?php/** * 冒泡排序算法实现(从小到大) */function maopaoSort (&$array){    $count = count($array);    for ($i = 0; $i < $count - 1; $i ++) {        for ($j = 0; $j < $count - $i - 1; $j ++) {            if ($array[$j] > $array[$j + 1]) {                $tmp = $array[$j];                $array[$j] = $array[$j + 1];                $array[$j + 1] = $tmp;            }        }    }}/** * 快速排序 */function pivotParation (&$array, $start, $end){    $stand = $array[$start];    while ($start &lt; $end) {        while ($start &lt; $end && $array[$end] >= $stand) {            $end --;        }        if ($start &lt; $end) {            $array[$start ++] = $array[$end];        }        while ($start &lt; $end && $array[$start] <= $stand) {            $start ++;        }        if ($start &lt; $end) {            $array[$end --] = $array[$start];        }    }    $array[$start] = $stand;    return $start;}function quickSort (&$array, $begin, $end){    if ($begin &lt; $end) {        $pivot = pivotParation($array, $begin, $end);        quickSort($array, $begin, $pivot - 1);        quickSort($array, $pivot + 1, $end);    }}$arr = array(        5,        1,        3,        2,        19,        11,        25,        12,        100,        10000,        12);// 冒泡排序maopaoSort($arr);print_r($arr);echo "<br>";// 快速排序$count = count($arr);quickSort($arr, 0, $count - 1);print_r($arr);


2.使用php描述顺序查找和二分查找

<?php/** * 顺序查找 */function seqSearch ($arr, $needle){    for ($i = 0, $len = count($arr); $i < $len; $i ++) {        if ($arr[$i] == $needle) {            return $i;        }    }    return - 1;}/** * 二分查找 */function midSearch ($arr, $start, $end, $needle){    while ($start &lt;= $end) {        $mid = (int)($start + ($end - $start) / 2); // 防止超出整数表示范围        if ($arr[$mid] == $needle) {            return $mid;        } else if ($arr[$mid] > $needle) {            $end = $mid - 1;        } else {            $start = $mid + 1;        }    }    return - 1;}$arr = array(        1,        2,        3,        4,        5,        6,        7,        8,        9,        10);$needle = 5;echo seqSearch($arr, $needle);echo "<br>";echo midSearch($arr, 0, count($arr) - 1, $needle);


3.写一个二维数组排序算法函数,能够具有通用性,可以调用php内置函数


/** * Description:获取中枢点的位置 * * @param array $array             * @param int $left             * @param int $right             * @param string $field             * @return int */function fetchArrayPivot (&$array, $left, $right, $field){    // 基准定义    $stand = $array[$left];    // 遍历数组    while ($left &lt; $right) {        while ($left &lt; $right && $array[$right][$field] &gt;= $stand[$field]) {            $right --;        }        if ($left &lt; $right) {            $array[$left ++] = $array[$right];        }        while ($left &lt; $right && $array[$left][$field] &lt;= $stand[$field]) {            $left ++;        }        if ($left &lt; $right) {            $array[$right --] = $array[$left];        }    }    // 获取中枢点位置    $array[$left] = $stand;    return $left;}/** * Description:快速排序主程序 * * @param array $array             * @param int $begin             * @param int $end             * @param string $field             */function quickSort (&$array, $begin, $end, $field){    // 变量定义    $pivot = null;    if ($begin &lt; $end) {        $pivot = fetchArrayPivot($array, $begin, $end, $field);        quickSort($array, $begin, $pivot - 1, $field);        quickSort($array, $pivot + 1, $end, $field);    }}

利用快排的思想,增加一个field参数

原创粉丝点击