基础面试1
M:代表自己的見(jiàn)解。? ?I:代表網(wǎng)絡(luò)查詢結(jié)果,答案
1、__FILE__表示什么意思?(5分)
?
M:__FILE__是系統(tǒng)中魔術(shù)常量中的一種,記錄了當(dāng)前文件夾的絕對(duì)路徑
?
I:?文件的完整路徑和文件名。如果用在包含文件中,則返回包含文件名。自 PHP 4.0.2 起,__FILE__ 總是包含一個(gè)絕對(duì)路徑,而在此之前的版本有時(shí)會(huì)包含一個(gè)相對(duì)路徑。
?
?
2、如何獲取客戶端的IP地址?(5分)
M:通過(guò)$_SERVER超全局變量來(lái)獲取, $_SERVER['remote_addr'] 請(qǐng)求中客戶端的ip地址
I:$_SERVER[‘REMOTE_ADDR’]
?
超全局變量中的參數(shù)詳解?https://www.cnblogs.com/rendd/p/6182918.html
?
?
3、寫(xiě)出使用header函數(shù)跳轉(zhuǎn)頁(yè)面的語(yǔ)句(5分)
M: header("404 http/1.1 頁(yè)面丟失了");
I:Header(‘location:index.php’);
?
4、$str是一段html文本,使用正則表達(dá)式去除其中的所有js腳本(5分)
M:.......
I:
$pattern = ‘/<script.*>\.+<\/script>/’;
Preg_replace($pattern,’’,$str);
? ? ??
?
?
5、寫(xiě)出將一個(gè)數(shù)組里的空值去掉的語(yǔ)句(5分)
M: 使用array_filter去除數(shù)組中的空值? ? ?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、寫(xiě)出獲取當(dāng)前時(shí)間戳的函數(shù),及打印前一天的時(shí)間的方法(格式:年-月-日 時(shí):分:秒) (5
分)
?
M: 獲取當(dāng)前時(shí)間戳 time(),? ? ?strtotime('Y年m月d日 H時(shí)i分s',time()-60*60*24)
I:Time();
Date(“Y-m-d H:i:s”,Strtotime(“-1 day”));
? ? ??
7、寫(xiě)出php進(jìn)行編碼轉(zhuǎn)換的函數(shù)(5分)
M: iconv('urf8','gbk',$str);
I:Iconv(‘utf-8’,’gb2312’,$str);
?
8、$str = “1,3,5,7,9,10,20”,使用什么函數(shù)可以把字符串str轉(zhuǎn)化為包含各個(gè)數(shù)字的數(shù)組
?(5分)
?M: 轉(zhuǎn)化為數(shù)組并轉(zhuǎn)換為int類(lèi)型? ?array_map('intval',implode(',',$str));
?I:$arr = explode(“,”,$str);
?
9、serialize() /unserialize()函數(shù)的作用(5分)
M: serialize 講一個(gè)對(duì)象序列化成一個(gè)字符串,方便存入數(shù)據(jù)庫(kù)或其他地方,unserialize 將序列化后的字符串進(jìn)行反序列化成一個(gè)對(duì)象??
I:serialize()和unserialize()在php手冊(cè)上的解釋是:
serialize — 產(chǎn)生一個(gè)可存儲(chǔ)的值的表示,返回值為字符串,此字符串包含了表示 value 的字節(jié)流,不丟失其類(lèi)型和結(jié)構(gòu),可以存儲(chǔ)于任何地方。
unserialize — 從已存儲(chǔ)的表示中創(chuàng)建 PHP 的值?
?
10、寫(xiě)出一個(gè)函數(shù),參數(shù)為年份和月份,輸出結(jié)果為指定月的天數(shù)(5分)
?M: 輸入年份,和月份返回制定月的天數(shù),只判斷了閏年的情況下??
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、一個(gè)文件的路徑為/wwwroot/include/page.class.php,寫(xiě)出獲得該文件擴(kuò)展名的方法(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、你使用過(guò)哪種PHP的模板引擎?(5分)
?M: smarty模板引擎
?
I:Smarty,thinkphp自帶的模板引擎
?
13、請(qǐng)簡(jiǎn)單寫(xiě)一個(gè)類(lèi),實(shí)例化這個(gè)類(lèi),并寫(xiě)出調(diào)用該類(lèi)的屬性和方法的語(yǔ)句(5分)
M:
? ? class Getman
? ? {
? ? ? ? ? public $name='楊濤';
? ? ? ? ? public $age=18;
? ? ? ? ? public $sex='男';
? ? ? ? ? public function getSex()
? ? ? ? ? {
? ? ? ? ? ? ? ? ?return $this->sex;
? ? ? ? ? }
? ? }
$object = new Getman();
//獲取性別屬性
$sex = $object->sex;
//通過(guò)方法獲取性別
$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數(shù)據(jù)庫(kù)db_test里已建有表friend,數(shù)據(jù)庫(kù)的連接用戶為root,密碼為123
M:? $link = mysqli_connect('127.0.0.1','root','123','db_test');
? ? ? ?連接數(shù)據(jù)庫(kù)
?
I:
14、本地mysql數(shù)據(jù)庫(kù)db_test里已建有表friend,數(shù)據(jù)庫(kù)的連接用戶為root,密碼為123
friend表字段為:id,name,age,gender,phone,email
請(qǐng)使用php連接mysql,選擇出friend表里age > 20的所有記錄打印結(jié)果,并統(tǒng)計(jì)出查詢出的結(jié)果總數(shù)。(5分)
<?php
$link = Mysql_connect(“l(fā)ocalhost”,”root”,”123”) or die(“數(shù)據(jù)庫(kù)連接失敗!”);
Mysql_select_db(“db_test”,$link) or die(“選擇數(shù)據(jù)庫(kù)失敗!”);
$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、以下有兩個(gè)表
user表 字段id (int),name (varchar)
score表 字段uid (int),subject (varchar) ,score (int)
score表的uid字段與user表的id字段關(guān)聯(lián)
要求寫(xiě)出以下的sql語(yǔ)句
1)在user表里新插入一條記錄,在score表里插入與新加入的記錄關(guān)聯(lián)的兩條記錄(5分)
?M:? ? insert into user(id,name) values(1,'yang');
? ? ? insert into score(uid,subject,score) values(1,'思想品德',89),(1,'語(yǔ)文',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)使用聯(lián)合查詢獲取name為“張三”的用戶的總分?jǐn)?shù)(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為“李四”的用戶,包括分?jǐn)?shù)記錄(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;
?
轉(zhuǎn)載于:https://www.cnblogs.com/yangtaog/p/11175360.html
總結(jié)