PHP基础面试题 – 第四天

PHP基础题 2020年3月12日 2.43K

1.写出下面的程序输出的结果

$str="cd";
$$str="abcde";
$$str.="ok";
echo $cd;

答案:该段代码输出是:ok

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

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

答案:结果为 5 0 1

3.请列举一些你所知道的开发模式 (如工厂模式,越多越好)

MVC模式、单态模式、敏捷开发模式、瀑布流模式、螺旋模式、值对象模式、注册模式、伪对象模式、策略模式、迭代器模式、规范模式

4.写一个函数,尽可能的高效,从一个标准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.求两个日期的差数,例如 2019-2-5 ~ 2020-3-6 的日期差数

方法一:先用strtotime转换成unix时间戳,然后相减,除以一天的秒数86400. 方法二:先用mktime转换成unix时间戳,然后相减,除以一天的秒数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()有什么区别?它们分别在什么情况下使用?

sort() 根据阵列中元素的值,以英文字母顺序排序,索引键会由 0 到 n-1 重新编号。主要是当阵列索引键的值无关疼痒时用来把阵列排序。

assort() PHP 没有 assort() 函式,所以可能是 asort() 的笔误。

asort() 与 sort() 一样把阵列的元素按英文字母顺序来排列,不同的是所有索引键都获得保留,特别适合替联想阵列排序。

ksort() 根据阵列中索引键的值,以英文字母顺序排序,特别适合用于希望把索引键排序的联想阵列。


关注微信公众号『PHP学习网

第一时间了解最新网络动态
关注博主不迷路~

PHP学习网:站内收集的部分资源来源于网络,若侵犯了您的合法权益,请联系我们删除!
分享到:
赞(0)

文章评论

您需要之后才可以评论
0点赞 0评论 收藏 QQ分享 微博分享

PHP学习网

PHP学习网