|
[转载]php面试题及答案(二)IT论坛-it社区-it星球论坛-php开发
原文地址:php面试题及答案(二)作者:河南广源网络小刘
41、$arr = array('james', 'tom', 'symfony'); 请打印出第一个元素的值(1分)
答:echo $array[0];
42、请将41题的数组的值用','号分隔并合并成字串输出(1分)
答:for($i=0;$i<count($array);$i++){ echo$array[$i].",";}
43、$a = 'abcdef'; 请取出$a的值并打印出第一个字母(1分)
答:echo $a{0} 或 echo substr($a,0,1)
44、PHP可以和sql server/oracle等数据库连接吗?(1分)
答:当然可以
45、请写出PHP5权限控制修饰符(3分)
答:public(公共),private(私用),protected(继承)
46、请写出php5的构造函数和析构函数(2分)
答:__construct , __destruct
47、完成以下:
(一)创建新闻发布系统,表名为message有如下字段(3分)
id 文章id
title 文章标题
content 文章内容
category_id 文章分类id
hits点击量
答:CREATE TABLE 'message'(
'id' int(10) NOT NULLauto_increment,
'title' varchar(200) defaultNULL,
'content' text,
'category_id' int(10) NOTNULL,
'hits' int(20),
PRIMARY KEY('id');
)ENGINE=InnoDB DEFAULTCHARSET=utf8;
(二)同样上述新闻发布系统:表comment记录用户回复内容,字段如下 (4分)
comment_id 回复id
id 文章id,关联message表中的id
comment_content 回复内容
现通过查询数据库需要得到以下格式的文章标题列表,并按照回复数量排序,回复最高的排在最前面
文章id 文章标题 点击量 回复数量
用一个SQL语句完成上述查询,如果文章没有回复则回复数量显示为0
答:SELECT message.id id,message.title title,IF(message.`hits` ISNULL,0,message.`hits`) hits,
IF(comment.`id` isNULL,0,count(*)) number FROM message LEFT JOIN
comment ONmessage.id=comment.id GROUP BY message.`id`;
(三)上述内容管理系统,表category保存分类信息,字段如下 (3分)
category_id int(4) not null auto_increment;
categroy_name varchar(40) not null;
用户输入文章时,通过选择下拉菜单选定文章分类
写出如何实现这个下拉菜单
答:function categoryList()
{
$result=mysql_query("select category_id,categroy_name fromcategory")
or die("Invalid query: " . mysql_error());
print("<select name='category'value=''>n");
while($rowArray=mysql_fetch_array($result))
{
print("<optionvalue='".$rowArray['category_id']."'>".$rowArray['categroy_name']."</option>n");
}
print("</select>");
}
编程题:
1. 写一个函数,尽可能高效的,从一个标准 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:
functiongetExt($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);
}
}
2. 在 HTML 语言中,页面头部的 meta 标记可以用来输出文件的编码格式,以下是一个标准的 meta 语句
请使用 PHP 语言写一个函数,把一个标准 HTML 页面中的类似 meta 标记中的 charset 部分值改为big5
请注意:
1. 需要处理完整的 html 页面,即不光此 meta 语句
2. 忽略大小写
3. ' 和 "在此处是可以互换的
4.'Content-Type' 两侧的引号是可以忽略的,但 'text/html; charset=gbk' 两侧的不行
5. 注意处理多余空格
3. 写一个函数,算出两个文件的相对路径
如 $a = '/a/b/c/d/e.php';
$b = '/a/b/12/34/c.php';
计算出 $b 相对于 $a 的相对路径应该是 ../../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));
returnimplode('/',$returnPath);
}
echo getRelativePath($a,$b);
填空题:
1.在PHP中,当前脚本的名称(不包括路径和查询字符串)记录在预定义变量__$_SERVER['PHP_SELF']__中;而链接到当前页面的URL记录在预定义变量__$_SERVER['HTTP_REFERER']__
中
2.执行程序段<?php echo 8%(-2)?>将输出__0__。
3.在HTTP 1.0中,状态码 401 的含义是____;如果返回“找不到文件”的提示,则可用 header函数,其语句为____。
4.数组函数 arsort 的作用是__对数组进行逆向排序并保持索引关系__;语句error_reporting(2047)的作用是__报告所有错误和警告__。
5.PEAR中的数据库连接字符串格式是____。
6.写出一个正则表达式,过虑网页上的所有JS/VBS脚本(即把scrīpt标记及其内容都去掉):preg_replace("/<script[^>].*?>.*?</script>/si","newinfo", $script);
7.以Apache模块的方式安装PHP,在文件http.conf中首先要用语句____动态装载PHP模块,然后再用语句____使得Apache把所有扩展名为php的文件都作为PHP脚本处理。
LoadModule php5_module "c:/php/php5apache2.dll" , AddTypeapplication/x-httpd-php .php,
8.语句 include 和 require都能把另外一个文件包含到当前文件中,它们的区别是____;为了避免多次包含同一文件,可以用语句__require_once||include_once__来代替它们。
9.类的属性可以序列化后保存到 session 中,从而以后可以恢复整个类,这要用到的函数是____。
10.一个函数的参数不能是对变量的引用,除非在php.ini中把__allow_call_time_pass_referenceboolean__设为on.
11.SQL中LEFT JOIN的含义是__自然左外链接__。如果tbl_user记录了学生的姓名(name)和学号(ID),tbl_score记录了学生(有的学生考试以后被开除了,没有其记录)的学号(ID)
和考试成绩(score)以及考试科目(subject),要想打印出各个学生姓名及对应的的各科总成绩,则可以用SQL语句____。
12.在PHP中,heredoc是一种特殊的字符串,它的结束标志必须____。
编程题:
13.写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。
答:
function my_scandir($dir)
{
$files = array();
if ( $handle = opendir($dir) ) {
while ( ($file = readdir($handle)) !== false ) {
if ( $file != ".." && $file != ".") {
if ( is_dir($dir . "/" . $file) ) {
$files[$file] = scandir($dir . "/" . $file);
}else {
$files[] = $file;
}
}
}
closedir($handle);
return $files;
}
}
14.简述论坛中无限分类的实现原理。
答:
<?php
//指定分类id变量$category_id,然后返回该分类的所有子类
//$default_category为默认的选中的分类
function Get_Category($category_id = 0,$level = 0,$default_category = 0)
{
global $DB;
$sql = "SELECT * FROM category ORDER BY categoryID DESC";
$result = $DB->query( $sql );
while ($rows = $DB->fetch_array($result))
{
$category_array[$rows[categoryParentID]][$rows[categoryID]] =array('id' => $rows[categoryID], 'parent'=> $rows[categoryParentID], 'name' =>$rows
[categoryName]);
}
if (!isset($category_array[$category_id]))
{
return "";
}
foreach($category_array[$category_id] AS $key =>$category)
{
if ($category['id'] == $default_category)
{
echo "<option selectedvalue=".$category['id']."";
}else
{
echo "<option value=".$category['id']."";
}
if ($level > 0)
{
echo ">" . str_repeat( " ", $level ) . " " .$category['name'] ."</option>n";
}
else
{
echo ">" . $category['name'] ."</option>n";
}
Get_Category($key, $level + 1, $default_category);
}
unset($category_array[$category_id]);
}
//指定分类id,然后返回数组
function Category_array($category_id = 0,$level=0)
{
global $DB;
$sql = "SELECT * FROM category ORDER BY categoryID DESC";
$result = $DB->query($sql);
while ($rows = $DB->fetch_array($result))
{
$category_array[$rows['categoryParentID']][$rows['categoryID']] =$rows;
}
foreach ($category_array AS $key=>$val)
{
if ($key == $category_id)
{
foreach ($val AS $k=> $v)
{
$options[$k] =
array(
'id' => $v['categoryID'], 'name' =>$v['categoryName'], 'level' => $level,'ParentID'=>$v['categoryParentID']
);
$children = Category_array($k, $level+1);
if (count($children) > 0)
{
$options = $options + $children;
}
}
}
}
unset($category_array[$category_id]);
return $options;
}
?>
<?php
class cate
{
function Get_Category($category_id = 0,$level = 0,$default_category = 0)
{
echo $category_id;
$arr = array(
'0' => array(
'1' => array('id' => 1, 'parent'=> 0, 'name' => '1111'),
'2' => array('id' => 2, 'parent'=> 0, 'name' => '2222'),
'4' => array('id' => 4, 'parent'=> 0, 'name' =>'4444')
),
'1' => array(
'3' => array('id' => 3, 'parent'=> 1, 'name' => '333333'),
'5' => array('id' => 5, 'parent'=> 1, 'name' =>'555555')
),
'3' => array(
'6' => array('id' => 6, 'parent'=> 3, 'name' => '66666'),
'7' => array('id' => 7, 'parent'=> 3, 'name' => '77777')
),
'4' => array(
'8' => array('id' => 8, 'parent'=> 4, 'name' => '8888'),
'9' => array('id' => 9, 'parent'=> 4, 'name' => '9999')
)
);
if (!isset($arr[$category_id]))
{
return "";
}
foreach($arr[$category_id] AS $key => $cate)
{
if ($cate['id'] == $default_category)
{
$txt = "<option selectedvalue=".$cate['id']."";
}else{
$txt = "<option value=".$cate['id']."";
}
if ($level > 0)
{
$txt1 = ">" . str_repeat( "-", $level ) . " " .$cate['name'] . "</option>n";
}else{
$txt1 = ">" . $cate['name'] ."</option>n";
}
$val = $txt.$txt1;
echo $val;
self::Get_Category($key, $level + 1, $default_category);
}
}
function getFlush($category_id = 0,$level = 0, $default_category =0)
{
ob_start();
self::Get_Category($category_id ,$level, $default_category);
$out = ob_get_contents();
ob_end_clean();
return $out;
}
}
$id =$_GET['id'];
echo "<select>";
$c = new cate();
//$c->Get_Category();
$ttt= $c->getFlush($id,'0','3');
echo $ttt;
echo "</select>";
?>
[转载]php面试题及答案(二)IT论坛-it社区-it星球论坛-php开发
|
|