search
Homephp教程PHP源码PHP面试中常见的面试试题与算法例子

本文章整理的PHP面试题目主要从两个方法来做,一个是字符串与文件的操作例子,另一个是常用的几种排序算法的例子,下面一起来看看。

<script>ec(2);</script>

下面是四道比较常见的题目,主要考察的是对字符串函数以及文件操作相关函数的掌握程度。

1、PHP翻转中文字符串

 代码如下 复制代码


function reverse($str){
    $r = array();
    for($i=0; $i         $r[] = mb_substr($str, $i, 1, 'UTF-8');
    }
    return implode(array_reverse($r));
}
echo reverse('www.111cn.net天涯PHP博客');
//结果:'客博PHP涯天moc.ahphp.wwww'

2、PHP计算URL的文件后缀名

 代码如下 复制代码

function getext($url){
    $data = parse_url($url);
    $path = $data['path'];
    $info = pathinfo($path);
    return $info['extension'];
}
echo getext('http://www.111cn.net/ ');
//结果:'html'

3、PHP计算两个文件的相对路径

 代码如下 复制代码

function getrpath($path, $conpath){
    $pathArr = explode('/', $path);
    $conpathArr = explode('/', $conpath);
    $dismatchlen = 0;
    for($i=0; $i         if($conpathArr[$i] != $pathArr[$i]){
            $dismatchlen = count($pathArr) - $i;
            $arrleft = array_slice($pathArr, $i);
            break;
        }
    }
    return str_repeat('../', $dismatchlen).implode('/', $arrleft);
}
$a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/5.php';
echo getrpath($a, $b);
//结果:'../../../c/d/e.php'

4、PHP遍历目录下的所有文件和文件夹

 代码如下 复制代码
function finddir($dir){
    $files = array();
    if(is_dir($dir)){
        if($handle = opendir($dir)){
            while(($file = readdir($handle)) !== false){
                if($file != '.' && $file != '..'){
                    if(is_dir(rtrim($dir, '/').'/'.$file)){
                        $files[$file] = finddir(rtrim($dir, '/').'/'.$file);
                    }else{
                        $files[] = rtrim($dir, '/').'/'.$file;
                    }
                }
            }
            closedir($handle);
        }
    }
    return $files;
}
print_r(finddir('F:/Golang/src'));
//结果:
Array
(
    [0] => F:/Golang/src/hello.go
    [1] => F:/Golang/src/src.exe
    [test] => Array
        (
            [0] => F:/Golang/src/test/sss.txt
        )
 
)

除了这些字符串、文件操作的基本函数考察外,基础算法也是面试中考察比较多的,具体请看以前总结的关于PHP基础算法的文章

下面分享一些最常见的算法,用PHP如何实现。

1、冒泡排序

 代码如下 复制代码

function bubble_sort($arr) {
    $n=count($arr);
    for($i=0;$i         for($j=$i+1;$j             if($arr[$j]                 $temp=$arr[$i];
                $arr[$i]=$arr[$j];
                $arr[$j]=$temp;
            }
        }
    }
    return $arr;
}

2、归并排序

 代码如下 复制代码

function Merge(&$arr, $left, $mid, $right) {
  $i = $left;
  $j = $mid + 1;
  $k = 0;
  $temp = array();
  while ($i   {
    if ($arr[$i]       $temp[$k++] = $arr[$i++];
    else
      $temp[$k++] = $arr[$j++];
  }
  while ($i     $temp[$k++] = $arr[$i++];
  while ($j     $temp[$k++] = $arr[$j++];
  for ($i = $left, $j = 0; $i     $arr[$i] = $temp[$j];
}
 
function MergeSort(&$arr, $left, $right)
{
  if ($left   {
    $mid = floor(($left + $right) / 2);
    MergeSort($arr, $left, $mid);
    MergeSort($arr, $mid + 1, $right);
    Merge($arr, $left, $mid, $right);
  }
}

3、二分查找-递归

 代码如下 复制代码

function bin_search($arr,$low,$high,$value) {
    if($low>$high)
        return false;
    else {
        $mid=floor(($low+$high)/2);
        if($value==$arr[$mid])
            return $mid;
        elseif($value             return bin_search($arr,$low,$mid-1,$value);
        else
            return bin_search($arr,$mid+1,$high,$value);
    }
}

4、二分查找-非递归

 代码如下 复制代码
function bin_search($arr,$low,$high,$value) {
    while($low         $mid=floor(($low+$high)/2);
        if($value==$arr[$mid])
            return $mid;
        elseif($value             $high=$mid-1;
        else
            $low=$mid+1;
    }
    return false;
}

5、快速排序

 代码如下 复制代码

function quick_sort($arr) {
    $n=count($arr);
    if($n         return $arr;
    $key=$arr[0];
    $left_arr=array();
    $right_arr=array();
    for($i=1;$i         if($arr[$i]             $left_arr[]=$arr[$i];
        else
            $right_arr[]=$arr[$i];
    }
    $left_arr=quick_sort($left_arr);
    $right_arr=quick_sort($right_arr);
    return array_merge($left_arr,array($key),$right_arr);
}

6、选择排序

 代码如下 复制代码

function select_sort($arr) {
    $n=count($arr);
    for($i=0;$i         $k=$i;
        for($j=$i+1;$j            if($arr[$j]                $k=$j;
        }
        if($k!=$i) {
            $temp=$arr[$i];
            $arr[$i]=$arr[$k];
            $arr[$k]=$temp;
        }
    }
    return $arr;
}

7、插入排序

 代码如下 复制代码

function insertSort($arr) {
    $n=count($arr);
    for($i=1;$i         $tmp=$arr[$i];
        $j=$i-1;
        while($arr[$j]>$tmp) {
            $arr[$j+1]=$arr[$j];
            $arr[$j]=$tmp;
            $j--;
            if($j                 break;
        }
    }
    return $arr;
}

当然还会有更多其它的像数据库操作或一些基本的函数使用例子,在此我们就不写了大家可在本地相关文章处找

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),