基础面试1
M:代表自己的见解。 I:代表网络查询结果,答案
1、__FILE__表示什么意思?(5分)
M:__FILE__是系统中魔术常量中的一种,记录了当前文件夹的绝对路径
I: 文件的完整路径和文件名。如果用在包含文件中,则返回包含文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径,而在此之前的版本有时会包含一个相对路径。
2、如何获取客户端的IP地址?(5分)
M:通过$_SERVER超全局变量来获取, $_SERVER['remote_addr'] 请求中客户端的ip地址
I:$_SERVER[‘REMOTE_ADDR’]
超全局变量中的参数详解 https://www.cnblogs.com/rendd/p/6182918.html
3、写出使用header函数跳转页面的语句(5分)
M: header("404 http/1.1 页面丢失了");
I:Header(‘location:index.php’);
4、$str是一段html文本,使用正则表达式去除其中的所有js脚本(5分)
M:.......
I:
$pattern = ‘/<script.*>\.+<\/script>/’;
Preg_replace($pattern,’’,$str);
5、写出将一个数组里的空值去掉的语句(5分)
M: 使用array_filter去除数组中的空值 array_filter($arr);
I:
第一种:$array1 = array(' ',1,'',2,3);
print_r(array_filter($array1, "del"));
function del($var)
{
return(trim($var));
}
第二种:
$arr=array("",1,2,3,"");
$ptn="/\S+/i";
print_r(preg_grep($ptn,$arr));
6、写出获取当前时间戳的函数,及打印前一天的时间的方法(格式:年-月-日 时:分:秒) (5
分)
M: 获取当前时间戳 time(), strtotime('Y年m月d日 H时i分s',time()-60*60*24)
I:Time();
Date(“Y-m-d H:i:s”,Strtotime(“-1 day”));
7、写出php进行编码转换的函数(5分)
M: iconv('urf8','gbk',$str);
I:Iconv(‘utf-8’,’gb2312’,$str);
8、$str = “1,3,5,7,9,10,20”,使用什么函数可以把字符串str转化为包含各个数字的数组
?(5分)
M: 转化为数组并转换为int类型 array_map('intval',implode(',',$str));
I:$arr = explode(“,”,$str);
9、serialize() /unserialize()函数的作用(5分)
M: serialize 讲一个对象序列化成一个字符串,方便存入数据库或其他地方,unserialize 将序列化后的字符串进行反序列化成一个对象
I:serialize()和unserialize()在php手册上的解释是:
serialize — 产生一个可存储的值的表示,返回值为字符串,此字符串包含了表示 value 的字节流,不丢失其类型和结构,可以存储于任何地方。
unserialize — 从已存储的表示中创建 PHP 的值
10、写出一个函数,参数为年份和月份,输出结果为指定月的天数(5分)
M: 输入年份,和月份返回制定月的天数,只判断了闰年的情况下
public function getSumDay($year,$month)
{
$num = $year%4;
switch ($month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
echo 31;
break;
case 2:
if($num===0){
echo 28;
}else{
echo 29;
}
break;
case 4:
case 6:
case 9:
case 11:
case 12:
echo 30;
break;
}
}
I:
Function day_count($year,$month){
Echo date(“t”,strtotime($year.”-”.$month.”-1”));
}
11、一个文件的路径为/wwwroot/include/page.class.php,写出获得该文件扩展名的方法(5
分)
M:
第一种:pathinfo('/wwwroot/include/page.class.php',PATHINFO_EXTENSION);
第二种: end(explode('.','/wwwroot/include/page.class.php'))或者array_pop(explode('.','/wwwroot/include/page.class.php'));
第三种:substr('/wwwroot/include/page.class.php',-3);
I:
$arr = pathinfo(“/wwwroot/include/page.class.php”);
$str = substr($arr[‘basename’],strrpos($arr[‘basename’],’.’));
12、你使用过哪种PHP的模板引擎?(5分)
M: smarty模板引擎
I:Smarty,thinkphp自带的模板引擎
13、请简单写一个类,实例化这个类,并写出调用该类的属性和方法的语句(5分)
M:
class Getman
{
public $name='杨涛';
public $age=18;
public $sex='男';
public function getSex()
{
return $this->sex;
}
}
$object = new Getman();
//获取性别属性
$sex = $object->sex;
//通过方法获取性别
$sex = $object->getSex();
unset($object);
I:
Class myclass{
Public $aaa;
Public $bbb;
Public function myfun(){
Echo “this is my function”;
}
}
$myclass = new myclass();
$myclass->$aaa;
$myclass->myfun();
14、本地mysql数据库db_test里已建有表friend,数据库的连接用户为root,密码为123
M: $link = mysqli_connect('127.0.0.1','root','123','db_test');
连接数据库
I:
14、本地mysql数据库db_test里已建有表friend,数据库的连接用户为root,密码为123
friend表字段为:id,name,age,gender,phone,email
请使用php连接mysql,选择出friend表里age > 20的所有记录打印结果,并统计出查询出的结果总数。(5分)
<?php
$link = Mysql_connect(“localhost”,”root”,”123”) or die(“数据库连接失败!”);
Mysql_select_db(“db_test”,$link) or die(“选择数据库失败!”);
$sql = “select id,name,age,gender,phone,email from friend where age>20”;
$result = mysql_query($sql);
$count = mysql_num_rows($result);
While($row = mysql_fetch_assoc($result)){
Echo $row[‘id’];
….
}
15、以下有两个表
user表 字段id (int),name (varchar)
score表 字段uid (int),subject (varchar) ,score (int)
score表的uid字段与user表的id字段关联
要求写出以下的sql语句
1)在user表里新插入一条记录,在score表里插入与新加入的记录关联的两条记录(5分)
M: insert into user(id,name) values(1,'yang');
insert into score(uid,subject,score) values(1,'思想品德',89),(1,'语文',90);
I:
1). mysql_query(“insert into user(name) values(‘test’)”);
$id = mysql_insert_id();
Mysql_query(“insert into score(uid,subjext,score) values(“.$id.”,’english’,’99’)”);
2)获取score表里uid为2的用户score最高的5条记录(5分)
M: select * from score where uid=2 limit 5 order by score desc;
I:2).$sql = select uid,sunjext,score from score where uid=2 order by score desc limit 0,5;
3)使用联合查询获取name为“张三”的用户的总分数(5分)
M: select sum(s.score) from score as s left join user as u on u.id = s.uid where u.name='张三';
I:3).select s.score from score s RIGHT JOIN user u ON u.id=s.uid where u.name=’张三;
4)删除name为“李四”的用户,包括分数记录(5分)
M: delete user,score from user,score where user.id=score.uid and user.name='李四';
I:
4).delete from score where uid in(select id from user where name=’李四’);
Delete from user where name=’李四’;
5)清空score表(5分)
M: turncate score;
I:5).delete from score;
6)删除user表(5分)
M: drop table user;
I:6).drop table user;

浙公网安备 33010602011771号