您的位置:首页 > 职场人生

PHP经典面试题汇总(续)

2010-04-20 11:02 423 查看
1、实现中文字串截取无乱码的方法?

答:function GBsubstr($string, $start, $length) {
if(strlen($string)>$length){
$str=null;
$len=$start+$length;
for($i=$start;$i<$len;$i++){
if(ord(substr($string,$i,1))>0xa0){
$str.=substr($string,$i,2);
$i++;
}else{
$str.=substr($string,$i,1);
}
}
return $str.'...';
}else{
return $string;
}
}

2.用PHP写出显示客户端IP与服务器IP的代码?
答:打印客户端IP:echo $_SERVER[‘REMOTE_ADDR’]; 或者: getenv('REMOTE_ADDR');
打印服务器IP:echo gethostbyname("www.bolaiwu.com");

3、如何修改SESSION的生存时间?
答:方法1:将php.ini中的session.gc_maxlifetime设置为9999重启apache
方法2:$savePath = "./session_save_dir/";
$lifeTime = 小时 * 秒;
session_save_path($savePath);
session_set_cookie_params($lifeTime);
session_start();
方法3:setcookie() and session_set_cookie_params($lifeTime);

4、有一个网页地址, 比如PHP开发资源网主页: http://www.phpres.com/index.html,如何得到它的内容?
答:方法1(对于PHP5及更高版本):
$readcontents = fopen("http://www.phpres.com/index.html", "rb");
$contents = stream_get_contents($readcontents);
fclose($readcontents);
echo $contents;
方法2:
echo file_get_contents("http://www.phpres.com/index.html");

5、在HTTP 1.0中,状态码401的含义是(?);如果返回“找不到文件”的提示,则可用 header 函数,其语句为(?);?
答:状态401代表未被授权,header("Location:www.xxx.php");

6.谈谈对mvc的认识?
答:由模型(model),视图(view),控制器(controller)完成的应用程序
由模型发出要实现的功能到控制器,控制器接收组织功能传递给视图;

7. 请说明php中传值与传引用的区别。什么时候传值什么时候传引用?
答:按值传递:函数范围内对值的任何改变在函数外部都会被忽略
按引用传递:函数范围内对值的任何改变在函数外部也能反映出这些修改
优缺点:按值传递时,php必须复制值。特别是对于大型的字符串和对象来说,这将会是一个代价很大的操作。
按引用传递则不需要复制值,对于性能提高很有好处。

8.在PHP中error_reporting这个函数有什么作用?
答:设置错误级别与错误信息回报

9. 请写一个函数验证电子邮件的格式是否正确?
答:function checkEmail($email)
{
$pregEmail = "/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i";
return preg_match($pregEmail,$email);
}

10、JS表单弹出对话框函数是?获得输入焦点函数是?
答:弹出对话框: alert(),prompt(),confirm()
获得输入焦点 focus();

11、JS的转向函数是?怎么引入一个外部JS文件?
答:window.location.href,<script type="text/javascript" src="js/js_function.js"></script>

12、mysql_fetch_row() 和mysql_fetch_array之间有什么区别?
答:mysql_fetch_row是从结果集取出1行数组,作为枚举
mysql_fetch_array是从结果集取出一行数组作为关联数组,或数字数组,两者兼得;

13、mysql_fetch_row() 和mysql_fetch_array之间有什么区别?
答:mysql_fetch_row是从结果集取出1行数组,作为枚举
mysql_fetch_array是从结果集取出一行数组作为关联数组,或数字数组,两者兼得;

14、请写出数据类型(int char varchar datetime text)的意思; 请问varchar和char有什么区别?
答:int是数字类型,char固定长度字符串,varchar实际长度字符串,datetime日期时间型,text文本字符串
char的场地固定为创建表设置的长度,varchar为可变长度的字符

15、取得查询结果集总数的函数是?
答:mysql_num_rows($result);

17、请写出php5的构造函数和析构函数?
答:__construct , __destruct

18. 写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名?
例如: http://www.sina.com.cn/abc/de/fg.php?id=1 需要取出 php 或 .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);
}
}

19. 写一个函数,算出两个文件的相对路径?
  如 $a = '/a/b/c/d/e.php';
  $b = '/a/b/12/34/c.php';
  计算出 $b 相对于 $a 的相对路径应该是 http://www.cnblogs.com/c/d将()添上 答:function getRelativePath($a, $b) {
$returnPath = array(dirname($b));
$arrA = explode('/', $a);
$arrB = explode('/', $returnPath[0]);
for ($n = 1, $len = count($arrB); $n < $len; $n++) {
if ($arrA[$n] != $arrB[$n]) {
break;
}
}
if ($len - $n > 0) {
$returnPath = array_merge($returnPath, array_fill(1, $len - $n, '..'));
}

$returnPath = array_merge($returnPath, array_slice($arrA, $n));
return implode('/', $returnPath);
}
echo getRelativePath($a, $b);

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: