溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP的相關(guān)面試題

發(fā)布時(shí)間:2021-04-06 11:49:16 來源:億速云 閱讀:160 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)PHP的相關(guān)面試題的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

1. 遍歷目錄下的所有文件及文件夾

function fileShow($dir){                           
    $handle = opendir($dir);                     // 打開目錄句柄
    while ($file = readdir($handle)) {           // 返回目錄句柄中的條目
        if ($file !== '..' && $file !== '.') {
            $f = $dir . '/' . $file;
            if (is_file($f)) {
                echo '|--' . $file . '<br>';     // 代表文件
            } else {
                echo '--' . $file . '<br>';      // 代表文件夾
                fileShow($f);
            }
        }
    }
}

2. 單例模式創(chuàng)建 mysqli 鏈接

class db
{
    private        $con;
    private static $instance;

    /**
     * 定義一個(gè)私有的構(gòu)造函數(shù),確保單例類不能通過 new 關(guān)鍵字實(shí)例化,只能被其自身實(shí)例化
     */
    private function __construct($host, $username, $password, $database)
    {
        $this->con = mysqli_connect($host, $username, $password);
        if (!$this->con) die("連接失敗!");
        mysqli_select_db($database);
    }

    /**
     * 創(chuàng)建單例
     */
    public static function getInstance($host, $username, $password, $database)
    {
        if (self::$instance) return self::$instance;
        self::$instance = new db($host, $username, $password, $database);
    }

    /**
     * 關(guān)閉數(shù)據(jù)庫(kù)連接
     */
    public function close()
    {
        return mysqli_close($this->con);
    }

    /**
     * 定義私有的__clone()方法,確保單例類不能被復(fù)制或克隆
     */
    private function __clone()
    {
    }
}

3. 獲取一個(gè)網(wǎng)頁(yè)地址的內(nèi)容

$url = "http://www.phpres.com/index.html";
$str = file_get_contents($url);

// 或 
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_exec($ch);
curl_close($ch);

4. 從 url  獲取文件的擴(kuò)展名

function getExt1 () {
    $url_path = "http://www.sina.com.cn/abc/de/fg.php?id=1";
    $temp = pathinfo($url_path, PATHINFO_EXTENSION);  //    php?id=1
    $temp = explode("?", $temp);
    echo $temp[0];  // php
}

function getExt2 () {
    $url_path = "http://www.sina.com.cn/abc/de/fg.php?id=1";
    // ["scheme" => "http","host" => "www.sina.com.cn","path" => "/abc/de/fg.php","query" => "id=1"]
    $temp = parse_url($url_path);
    echo pathinfo($temp['path'], PATHINFO_EXTENSION);
}

5. 獲取一個(gè)文件的擴(kuò)展名

function get_ext1 ($file_name) {
    return strrchr($file_name, '.');                     // .jpg
}

function get_ext2 ($file_name) {
    return substr($file_name, strrpos($file_name, '.')); // .jpg
}

function get_ext3 ($file_name) {
    $tmp = explode('.', $file_name);
    return array_pop($tmp);                              // jpg
}

function get_ext4 ($file_name) {
    return pathinfo($file_name, PATHINFO_EXTENSION);    // jpg
}

6. 轉(zhuǎn)換駝峰式字符串

function conversion1 () { 
    $str = "open_door";
    return ucwords(str_replace('_',' ',$str)); // Open Door}

7. header( ) 跳轉(zhuǎn)頁(yè)面

header('location:index.php');     // 跳轉(zhuǎn)頁(yè)面
header("HTTP/1.1 404 Not Found"); // 實(shí)現(xiàn)頁(yè)面404錯(cuò)誤提示功能

8. 使用正則表達(dá)式去除其中的所有js腳本

# $str是一段html文本
$pattern = '/<script.*>.*<\/script>/i';
preg_replace($pattern,'',$str);

9. 將一個(gè)數(shù)組里的空值去掉

\S 匹配任何非空白字符。i不區(qū)分大小寫

$arr = array("",1,2,3,"");
$ptn = "/\S+/i";  
print_r(preg_grep($ptn,$arr));

10. 給出年份和月份,輸出指定月份的天數(shù)

function dayCount($year, $month) {
    echo date('t', strtotime($year . '-' . $month . '-1'));
}

11. 編寫函數(shù)取得上一月的最后一天

function get_last_month_last_day(){
    $date = "2021-2-18";
    $time = strtotime($date);
    $day  = date('j',$time); // 獲取該日期是當(dāng)前月的第幾天
    return date('Y-m-d',strtotime("-{$day} days",$time));}

12. 確保多個(gè)進(jìn)程同時(shí)寫入同一個(gè)文件成功

$fp = fopen("lock.txt","w+");
if(flock($fp, LOCK_EX)) {          // 寫鎖
    fwrite($fp, "write something");
    flock($fp, LOCK_UN);           // 解鎖
} else {
    echo "file is locking";
}
fclose($fp);

13. 將字符串08/26/2003替換為2003-08-26

 $str     = '08/26/2003';
 $pattern = '/(\d+)\/(\d+)\/(\d+)/';
 $rep     = '$3-$1-$2';
 echo preg_replace($pattern, $rep, $str);

14. 求兩個(gè)日期的差數(shù)

// 方法一:用DateTime類
$day1 = '2003-09-16';
$day2 = '2011-11-23';
$d1 = new dateTime($day1);
$d2 = new dateTime($day2);
echo $d1->diff($d2)->days;

// 方法二,用時(shí)間戳計(jì)算
echo (strtotime($day2) - strtotime($day1))/(86400);

感謝各位的閱讀!關(guān)于“PHP的相關(guān)面試題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI