1.寫出下面的程序輸出的結(jié)果
$str=”cd”;
$str=”abcde”;
$str.=”ok”;
echo $cd;
答案:該段代碼輸出是:ok
2.寫出如下程序的輸出結(jié)果
$count=5;
function get_count(){
static $count=0;
return $count++;
}
echo $count;
++$count;
echo get_count();
echo get_count();
答案:結(jié)果為 5 0 1
3.請列舉一些你所知道的開發(fā)模式 (如工廠模式,越多越好)
MVC模式、單態(tài)模式、敏捷開發(fā)模式、瀑布流模式、螺旋模式、值對象模式、注冊模式、偽對象模式、策略模式、迭代器模式、規(guī)范模式
4.寫一個函數(shù),盡可能地高效,從一個標(biāo)準(zhǔn)url 里取出文件的擴展名。例如:http:// cn.yahoo.com / abc /de/fg.php?id=1 需要取出 .php
答案1:
function getExt($url){
$arr = parse_url($url);
$file = basename($arr[‘path’]);
$ext = explode(“.”,$file);
return $ext[1];
}
答案2:
function getExt($url) {
$url = basename($url);
$pos1 = strpos($url,”.”);
$pos2 = strpos($url,”?”);
if(strstr($url,”?”)){
return substr($url,$pos1 + 1,$pos2 – $pos1 – 1);
} else {
return substr($url,$pos1);
}
}
5.求兩個日期的差數(shù),例如 2019-2-5 ~ 2020-3-6 的日期差數(shù)
方法一:先用strtotime轉(zhuǎn)換成unix時間戳,然后相減,除以一天的秒數(shù)86400. 方法二:先用mktime轉(zhuǎn)換成unix時間戳,然后相減,除以一天的秒數(shù)86400.
具體代碼如下:
方法一:
class Dtime
{
function get_days($date1, $date2)
{
$time1 = strtotime($date1);
$time2 = strtotime($date2);
return ($time2-$time1)/86400;
}
}
$Dtime = new Dtime;
echo $Dtime->get_days(‘2019-2-5’, ‘2020-3-6’);
方法二:
$temp = explode(‘-‘, ‘2007-2-5’);
$time1 = mktime(0, 0, 0, $temp[1], $temp[2], $temp[0]);
$temp = explode(‘-‘, ‘2007-3-6’);
$time2 = mktime(0, 0, 0, $temp[1], $temp[2], $temp[0]);
echo ($time2-$time1)/86400;
6.Sort() assort() ksort()有什么區(qū)別?它們分別在什么情況下使用?
sort() 根據(jù)陣列中元素的值,以英文字母順序排序,索引鍵會由 0 到 n-1 重新編號。主要是當(dāng)陣列索引鍵的值無關(guān)疼癢時用來把陣列排序。
assort() PHP 沒有 assort() 函式,所以可能是 asort() 的筆誤。
asort() 與 sort() 一樣把陣列的元素按英文字母順序來排列,不同的是所有索引鍵都獲得保留,特別適合替聯(lián)想陣列排序。
ksort() 根據(jù)陣列中索引鍵的值,以英文字母順序排序,特別適合用于希望把索引鍵排序的聯(lián)想陣列。