0
点赞
收藏
分享

微信扫一扫

[李景山php] php常见面试题


Php
冒泡排序法
$arr = array('22','2','4','355','1','123','33','55','66','3');
$count = count($arr);
for($i=0;$i<$count;$i++){
for($j=0;$j<$count-1;$j++){
if($arr[$j] > $arr[$j+1]){
$temp = $arr[$j+1];
$arr[$j+1] = $arr[$j];
$arr[$j] = $temp;
}
}
}
var_dump($arr);

快速排序法
$arr = array('312','322','645','321','22','3','4442');
$arr = quickArr($arr);
var_dump($arr);
function quickArr($arr=null){
$count = count($arr);
if($count <= 1) return $arr;
$key = $arr['0'];
$arr_left = array();
$arr_right = array();
for($i = 1;$i < $count;$i++){
if($arr[$i] <= $key)
$arr_left[] = $arr[$i];
else
$arr_right[] = $arr[$i];
}
$arr_left = quickArr($arr_left);
$arr_right = quickArr($arr_right);

return array_merge($arr_left,array($key),$arr_right);
}
复杂单表SQL
数据库test有一张user表,字段为id(序号),name(姓名),class(班级),score(分数),
1)写一句SQL,查出全部学生的分数,按降序排序
Select * from user order by score DESC
2)写一句SQL查出每个班的及格人数和不及格人数,格式为:班级 及格人数 不及格人数
select A.class,A.及格人数,B不及格人数 from (select class,clount(*) as 及格人数 from user where score>=60 group by class) as A,(select class,count(*) as 不及格人数 from user where score<60 group by class) as B where A.class=B.class.







select A.class,A.及格,B.不及格 from (select class,count(*) as 及格人数 from user where score >= 60 group by class) as A,(select class,count(*) as 不及格人数 from user where score < 60 group by class) as B where A.class=B.class


select A.class,A.及格人数,B.不及格人数 from (select class,count(*) as 及格人数 from user where score >=60 group by class) as A,(select class,count(*) as 不及格人数 from user where score<60 group by class ) as B where A.class=B.class.
3)用PHP执行第二小题的SQL,要求判断错误,显示结果,关闭数据库
try{
$dbid = new PDO('mysql:host=localhost;dbname=test','root','098098');
$dbid -> query('SET NAMES UTF8');
}catch(PDOException $e){
die($e->getmessage());
}
$stmt = $dbid -> query('select A.class,A.及格,B.不及格 from (select class,count(*) as 及格 from user where score >= 60 group by class) as A,(select class,count(*) as 不及格 from user where score < 60 group by class) as B where A.class=B.class');
if(!$stmt)
die('数据库错误');
$result = $stmt -> FETCHALL(PDO::FETCH_ASSOC);
if($result){
foreach($result as $val){
echo '班级:'.$val['class'].'及格人数'.$val['及格'].'不及格人数'.$val['不及格'].'<br />';
}
}else{
die('执行失败');
}
//PDO不需要关闭数据库 非要关可以这样
$dbid = null;

请用正则表达式写一个验证邮箱的PHP程序
$mail = '2-3_2@vip.qq.com';
$pattern = '#^([0-9A-Za-z-_]+@([0-9A-Za-z-\.]+\.([A-Za-z]){2,3}))$#';
$result = preg_match($pattern,$mail);
if($result)
echo '匹配成功';
else
echo '匹配失败';
以空格作为分隔将字符串 ‘aaa sss ccc eee ww ssq acc’拆分为数组并按字母升序排序,要求所有元素均为小写
$str = 'asd adsds dwew vbfgf reere';
var_dump(arr2sort($str));

function arr2sort($str){
if(empty($str)) return false;
$arr = explode(' ',strtolower($str));
if(count($arr) > 1){
sort($arr);
return $arr;
}
return $arr;
}

正则匹配一段str只允许大于0的数字和英文逗号,不符合的输入有提示信息
$str = '12033,243,2';
$pattern = '#^([1-9,])+#';
if(preg_match($pattern,$str))
echo '输入正确';
else
echo '输入错误';

将一个数组保存为文件再从文件中读出这个数组

$arr = array('123','sdasd','ewqewq','rewrefv','rwervv','rewrwer');

$arr = file2arr('haha.xxx');
var_dump($arr);
function arr2file($arr){
if(is_array($arr)){
$arr = implode(' ',$arr);
}
return file_put_contents('haha.xxx',$arr);
}

function file2arr($fileName){
if(!file_exists($fileName)) return false;
$str = file_get_contents($fileName);
return explode(' ',$str);
}
$result = arr2file($arr);
if($result)
echo '写入成功';
else
echo '写入失败';


算出两个文件的相对路径
$a = 'http://www.abc.com/1/2/3/4/5.php';
$b = 'http://www.abc.com/1/2/a/c/vv.php';

$a = parse_url($a);
$b = parse_url($b);

$aPath = ltrim($a['path'],'/');
$bPath = ltrim($b['path'],'/');

$aArr = explode('/',$aPath);
$bArr = explode('/',$bPath);

//清除掉两个URL一样的目录 比如/1/2
$countA = count($aArr);
for($i = 0; $i<$countA;$i++){
if($aArr[$i] == $bArr[$i]){
unset($aArr[$i]);
unset($bArr[$i]);
}else{
break;
}
}
//把URL a剩下的目录换成 .. 然后拼接B
$countA = count($aArr);
for($i = 0;$i<$countA;$i++)
$url[] = '..';
$newURL = rtrim(implode('/',$url),'/').'/'.implode('/',$bArr);
echo $newURL;

写一个函数能够遍历一个文件夹中所有文件和子文件夹
function showDIR($name){
if(!is_dir($name)) die('这不是一个目录!');
$handle = opendir($name);
while($n = readdir($handle)){
if($n == '.' || $n == '..') continue;
$fileName = $name.'/'.$n;
if(is_dir($fileName))
showDIR($fileName);
else
echo $fileName.'<br />';
}
}

提取出一段URL的文件后缀
$url = 'http://www.abc.com/index.JSP?hh=haha&ye=heihei';

$arr = parse_url($url);
$path = explode('.',$arr['path']);
echo array_pop($path);

求日期的差数
$date1 = '2012-2-10';
$date2 = '2012-12-21';

$time1 = strtotime($date1);
$time2 = strtotime($date2);

echo abs($time1-$time2)/(60*60*24);

找出一个数组最大值
$arr = array('11111','22222222','333333333','4444444444444','555555555','4324');
//不让用排序函数就用冒泡或快速排序
rsort($arr);
echo $arr['0'];

echo max(array('11111','22222222','333333333','4444444444444','555555555','4324'));

读取出<haha></haha>里面的内容,并将<haha>和</haha>替换为<b></b>
$str = '<a href="#">adsdasdasd</a><haha>1232rrrrr1312</haha><i>1213213<haha>哦耶</haha>dasdsa</i>';
//取出匹配到的数据
$pattern = '#<haha>(.*?)</haha>#s';
preg_match_all($pattern,$str,$arr);
echo '<pre>';
var_dump($arr);
echo '</pre>';
//替换匹配到的数据
$pattern2 = '#<(\/)?(haha)>#';
$re = '<${1}b>';
$arr2 = preg_replace($pattern2,$re,$str);
echo '<pre>';
var_dump($arr2);
echo '</pre>';

将字符串open_door转换成OpenDoor
$str = 'open_door';
$arr = explode('_',$str);
foreach($arr as $k => $v)
$arr[$k] = ucfirst($v);
echo implode('',$arr);

不乱码截取中文字符串
//允许使用mb_substr时
$str = '一二三四五六七';
echo mb_substr($str,3,1,'utf-8');
//不允许使用mb_substr时
function mysub($str,$start,$len='1',$charset='utf-8'){
if($charset=='utf-8')
$strlen = 3;
else
$strlen = 2;
$len = $strlen*$len;//得到字符串长度
$start = ($start-1)*$len;//得到截取起点
return substr($str,$start,$len);
}
echo mysub($str,2,3)

得到访客IP地址,并判断是否属于192.168.90.100 – 192.168.90.150

$ip = $_SERVER['REMOTE_ADDR'];
$numIP = str_replace('.','',$ip);
if($numIP >= 19216890100 && $numIP <= 19216890150)
echo '这个IP在范围内';
echo '不在范围内';

关联查询asc
有一张表user其中有字段uid和name,另一张表txt里面有字段tid,uid,times,title,contents
要求查询出每个用户的名字以及他一共发表了多少篇文章
try{
$dbid = new PDO('mysql:host=localhost;dbname=haha','root','098098');
$dbid -> query('SET NAMES UTF8');
}catch(PDOException $e){
die($e -> getMessage());
}


$stmt = $dbid->query('select A.name,B.postall from (select uid,name from user ) as A,(select uid,count(*) as postall from txt group by uid) as B where B.uid=A.uid');
if($stmt){
$result = $stmt -> FETCHALL(PDO::FETCH_ASSOC);
echo '<pre>';
var_dump($result);
echo '</pre>';
}
写个留言板

排除重复的数据行
Select distinct name from user
查一个字段的重复值
select name,count(*) as num from user group by name having count(*) > 1
查出一个表的结果 然后写入另一个表
insert into haha (name,num) select A.name,A.num from (select name,count(*) as num from user group by name having num > 1) as A
查出一个公司的名称,并通过id关联的cid统计出另一张表的职位个数
select A.name,B.num from (select id,name from comname) as A,(select cid,count(*) as num from job group by cid) as B where A.id=B.cid
将数据库的一个字段存的时间戳 转换为 日期
select A.name,A.num,A.times from (select name,num,from_unixtime(num) as times from haha) as A
根据时间戳查出这个月的纪录
select A.name,A.num,A.times from (select name,num,date_format(from_unixtime(num),\'%y-%m-%d\') as times from haha) as A where A.times=date_format(now(),\'%y-%m-%d\'


举报

相关推荐

0 条评论