比较基础的php面试题及答案 填空题部分 PHP面试题

<p>比较基础的php面试题及答案 填空题部分 PHP面试题<br /> 填空题:<br /> 1.在PHP中,当前脚本的名称(不包括路径和查询字符串)记录在预定义变量__$_SERVER['PHP_SELF']__中;而链接到当前页面的URL记录在预定义变量__$_SERVER['HTTP_REFERER']__<br /> 中<br /> 2.执行程序段<!--?php echo 8%(-2) ?-->将输出__0__。<br /> 3.在HTTP 1.0中,状态码 401 的含义是____;如果返回&ldquo;找不到文件&rdquo;的提示,则可用 header 函数,其语句为____。<br /> 4.数组函数 arsort 的作用是__对数组进行逆向排序并保持索引关系__;语句 error_reporting(2047)的作用是__报告所有错误和警告__。<br /> 5.PEAR中的数据库连接字符串格式是____。<br /> 6.写出一个正则表达式,过虑网页上的所有JS/VBS脚本(即把scrīpt标记及其内容都去掉):preg_replace(&rdquo;/<script[^>].*?>.*?</script>/si&rdquo;, &ldquo;newinfo&rdquo;, $script);<br /> 7.以Apache模块的方式安装PHP,在文件http.conf中首先要用语句____动态装载PHP模块,然后再用语句____使得Apache把所有扩展名为php的文件都作为PHP脚本处理。<br /> LoadModule php5_module &ldquo;c:/php/php5apache2.dll&rdquo; , AddType application/x-httpd-php .php,<br /> 8.语句 include 和 require 都能把另外一个文件包含到当前文件中,它们的区别是____;为了避免多次包含同一文件,可以用语句__require_once||include_once__来代替它们。<br /> 9.类的属性可以序列化后保存到 session 中,从而以后可以恢复整个类,这要用到的函数是____。<br /> 10.一个函数的参数不能是对变量的引用,除非在php.ini中把__allow_call_time_pass_reference boolean__设为on.<br /> 11.SQL中LEFT JOIN的含义是__自然左外链接__。如果 tbl_user记录了学生的姓名(name)和学号(ID),tbl_score记录了学生(有的学生考试以后被开除了,没有其记录)的学号(ID)<br /> 和考试成绩(score)以及考试科目(subject),要想打印出各个学生姓名及对应的的各科总成绩,则可以用SQL语句____。<br /> 12.在PHP中,heredoc是一种特殊的字符串,它的结束标志必须____。<br /> 编程题:<br /> 13.写一个函数,能够遍历一个文件夹下的所有文件和子文件夹。<br /> 答:<br /> function my_scandir($dir)<br /> {<br /> $files = array();<br /> if ( $handle = opendir($dir) ) {<br /> while ( ($file = readdir($handle)) !== false ) {<br /> if ( $file != &ldquo;..&rdquo;&amp;&amp; $file != &ldquo;.&rdquo; ) {<br /> if ( is_dir($dir . &ldquo;/&rdquo; . $file) ) {<br /> $files[$file] = scandir($dir . &ldquo;/&rdquo; . $file);<br /> }else {<br /> $files[] = $file;<br /> }<br /> }<br /> }<br /> closedir($handle);<br /> return $files;<br /> }<br /> }<br /> 14.简述论坛中无限分类的实现原理。<br /> 答:<br /> <!--?php<br /--> /*<br /> 数据表结构如下:<br /> CREATE TABLE `category` (<br /> `categoryID` smallint(5) unsigned NOT NULL auto_increment,<br /> `categoryParentID` smallint(5) unsigned NOT NULL default &lsquo;0&prime;,<br /> `categoryName` varchar(50) NOT NULL default &rdquo;,<br /> PRIMARY KEY (`categoryID`)<br /> ) ENGINE=MyISAM DEFAULT CHARSET=gbk;<br /> INSERT INTO `category` ( `categoryParentID`, `categoryName`) VALUES<br /> (0, &lsquo;一级类别&rsquo;),<br /> (1, &lsquo;二级类别&rsquo;),<br /> (1, &lsquo;二级类别&rsquo;),<br /> (1, &lsquo;二级类别&rsquo;),<br /> (2, &lsquo;三级类别&rsquo;),<br /> (2, &lsquo;333332&prime;),<br /> (2, &lsquo;234234&prime;),<br /> (3, &lsquo;aqqqqqd&rsquo;),<br /> (4, &lsquo;哈哈&rsquo;),<br /> (5, &lsquo;66333666&prime;);<br /> */<br /> //指定分类id变量$category_id,然后返回该分类的所有子类<br /> //$default_category为默认的选中的分类<br /> function Get_Category($category_id = 0,$level = 0, $default_category = 0)<br /> {<br /> global $DB;<br /> $sql = &ldquo;SELECT * FROM category ORDER BY categoryID DESC&rdquo;;<br /> $result = $DB-&gt;query( $sql );<br /> while ($rows = $DB-&gt;fetch_array($result))<br /> {<br /> $category_array[$rows[categoryParentID]][$rows[categoryID]] = array(&rsquo;id&rsquo; =&gt; $rows[categoryID], &lsquo;parent&rsquo; =&gt; $rows[categoryParentID], &lsquo;name&rsquo; =&gt; $rows<br /> [categoryName]);<br /> }<br /> if (!isset($category_array[$category_id]))<br /> {<br /> return &ldquo;&rdquo;;<br /> }<br /> foreach($category_array[$category_id] AS $key =&gt; $category)<br /> {<br /> if ($category['id'] == $default_category)<br /> {<br /> echo &ldquo; <option selected="" value="&rdquo;.$category['id'].&rdquo;&quot;;&lt;br"></option> }else<br /> {<br /> echo &ldquo; <option value="&rdquo;.$category['id'].&rdquo;&quot;;&lt;br"></option> }<br /> if ($level &gt; 0)<br /> {<br /> echo &ldquo;&gt;&rdquo; . str_repeat( &rdquo; &ldquo;, $level ) . &rdquo; &rdquo; . $category['name'] . &ldquo;n&rdquo;;<br /> }<br /> else<br /> {<br /> echo &ldquo;&gt;&rdquo; . $category['name'] . &ldquo;n&rdquo;;<br /> }<br /> Get_Category($key, $level + 1, $default_category);<br /> }<br /> unset($category_array[$category_id]);<br /> }<br /> /*<br /> 函数返回的数组格式如下所示:<br /> Array<br /> (<br /> [1] =&gt; Array ( [id] =&gt; 1 [name] =&gt; 一级类别 [level] =&gt; 0 [ParentID] =&gt; 0 )<br /> [4] =&gt; Array ( [id] =&gt; 4 [name] =&gt; 二级类别 [level] =&gt; 1 [ParentID] =&gt; 1 )<br /> [9] =&gt; Array ( [id] =&gt; 9 [name] =&gt; 哈哈 [level] =&gt; 2 [ParentID] =&gt; 4 )<br /> [3] =&gt; Array ( [id] =&gt; 3 [name] =&gt; 二级类别 [level] =&gt; 1 [ParentID] =&gt; 1 )<br /> [8] =&gt; Array ( [id] =&gt; 8 [name] =&gt; aqqqqqd [level] =&gt; 2 [ParentID] =&gt; 3 )<br /> [2] =&gt; Array ( [id] =&gt; 2 [name] =&gt; 二级类别 [level] =&gt; 1 [ParentID] =&gt; 1 )<br /> [7] =&gt; Array ( [id] =&gt; 7 [name] =&gt; 234234 [level] =&gt; 2 [ParentID] =&gt; 2 )<br /> [6] =&gt; Array ( [id] =&gt; 6 [name] =&gt; 333332 [level] =&gt; 2 [ParentID] =&gt; 2 )<br /> [5] =&gt; Array ( [id] =&gt; 5 [name] =&gt; 三级类别 [level] =&gt; 2 [ParentID] =&gt; 2 )<br /> [10] =&gt; Array ( [id] =&gt; 10 [name] =&gt; 66333666 [level] =&gt; 3 [ParentID] =&gt; 5 )<br /> )<br /> */<br /> //指定分类id,然后返回数组<br /> function Category_array($category_id = 0,$level=0)<br /> {<br /> global $DB;<br /> $sql = &ldquo;SELECT * FROM category ORDER BY categoryID DESC&rdquo;;<br /> $result = $DB-&gt;query($sql);<br /> while ($rows = $DB-&gt;fetch_array($result))<br /> {<br /> $category_array[$rows['categoryParentID']][$rows['categoryID']] = $rows;<br /> }<br /> foreach ($category_array AS $key=&gt;$val)<br /> {<br /> if ($key == $category_id)<br /> {<br /> foreach ($val AS $k=&gt; $v)<br /> {<br /> $options[$k] =<br /> array(<br /> &lsquo;id&rsquo; =&gt; $v['categoryID'], &lsquo;name&rsquo; =&gt; $v['categoryName'], &lsquo;level&rsquo; =&gt; $level, &lsquo;ParentID&rsquo;=&gt;$v['categoryParentID']<br /> );<br /> $children = Category_array($k, $level+1);<br /> if (count($children) &gt; 0)<br /> {<br /> $options = $options + $children;<br /> }<br /> }<br /> }<br /> }<br /> unset($category_array[$category_id]);<br /> return $options;<br /> }<br /> ?&gt;<br /> <!--?php<br /--> class cate<br /> {<br /> function Get_Category($category_id = 0,$level = 0, $default_category = 0)<br /> {<br /> echo $category_id;<br /> $arr = array(<br /> &lsquo;0&prime; =&gt; array(<br /> &lsquo;1&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 1, &lsquo;parent&rsquo; =&gt; 0, &lsquo;name&rsquo; =&gt; &lsquo;1111&prime;),<br /> &lsquo;2&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 2, &lsquo;parent&rsquo; =&gt; 0, &lsquo;name&rsquo; =&gt; &lsquo;2222&prime;),<br /> &lsquo;4&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 4, &lsquo;parent&rsquo; =&gt; 0, &lsquo;name&rsquo; =&gt; &lsquo;4444&prime;)<br /> ),<br /> &lsquo;1&prime; =&gt; array(<br /> &lsquo;3&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 3, &lsquo;parent&rsquo; =&gt; 1, &lsquo;name&rsquo; =&gt; &lsquo;333333&prime;),<br /> &lsquo;5&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 5, &lsquo;parent&rsquo; =&gt; 1, &lsquo;name&rsquo; =&gt; &lsquo;555555&prime;)<br /> ),<br /> &lsquo;3&prime; =&gt; array(<br /> &lsquo;6&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 6, &lsquo;parent&rsquo; =&gt; 3, &lsquo;name&rsquo; =&gt; &lsquo;66666&prime;),<br /> &lsquo;7&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 7, &lsquo;parent&rsquo; =&gt; 3, &lsquo;name&rsquo; =&gt; &lsquo;77777&prime;)<br /> ),<br /> &lsquo;4&prime; =&gt; array(<br /> &lsquo;8&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 8, &lsquo;parent&rsquo; =&gt; 4, &lsquo;name&rsquo; =&gt; &lsquo;8888&prime;),<br /> &lsquo;9&prime; =&gt; array(&rsquo;id&rsquo; =&gt; 9, &lsquo;parent&rsquo; =&gt; 4, &lsquo;name&rsquo; =&gt; &lsquo;9999&prime;)<br /> )<br /> );<br /> if (!isset($arr[$category_id]))<br /> {<br /> return &ldquo;&rdquo;;<br /> }<br /> foreach($arr[$category_id] AS $key =&gt; $cate)<br /> {<br /> if ($cate['id'] == $default_category)<br /> {<br /> $txt = &ldquo; <option selected="" value="&rdquo;.$cate['id'].&rdquo;&quot;;&lt;br"></option> }else{<br /> $txt = &ldquo; <option value="&rdquo;.$cate['id'].&rdquo;&quot;;&lt;br"></option> }<br /> if ($level &gt; 0)<br /> {<br /> $txt1 = &ldquo;&gt;&rdquo; . str_repeat( &ldquo;-&rdquo;, $level ) . &rdquo; &rdquo; . $cate['name'] . &ldquo;n&rdquo;;<br /> }else{<br /> $txt1 = &ldquo;&gt;&rdquo; . $cate['name'] . &ldquo;n&rdquo;;<br /> }<br /> $val = $txt.$txt1;<br /> echo $val;<br /> self::Get_Category($key, $level + 1, $default_category);<br /> }<br /> }<br /> function getFlush($category_id = 0,$level = 0, $default_category = 0)<br /> {<br /> ob_start();<br /> self::Get_Category($category_id ,$level, $default_category);<br /> $out = ob_get_contents();<br /> ob_end_clean();<br /> return $out;<br /> }<br /> }<br /> $id =$_GET['id'];<br /> echo &ldquo;<select>&rdquo;; $c = new cate(); //$c-&gt;Get_Category(); $ttt= $c-&gt;getFlush($id,&rsquo;0&prime;,&rsquo;3&prime;); echo $ttt; echo &ldquo;</select>&rdquo;;<br /> ?&gt;<br /> <br /> PHP笔试题 <br /> 1. 写出如下程序的输出结果<br /> <!--?<br /--> $str1 = null;<br /> $str2 = false;<br /> echo $str1==$str2 ? &lsquo;相等&rsquo; : &lsquo;不相等&rsquo;;<br /> //相等<br /> $str3 = &rdquo;;<br /> $str4 = 0;<br /> echo $str3==$str4 ? &lsquo;相等&rsquo; : &lsquo;不相等&rsquo;;<br /> //相等<br /> $str5 = 0;<br /> $str6 = &lsquo;0&prime;;<br /> echo $str5===$str6 ? &lsquo;相等&rsquo; : &lsquo;不相等&rsquo;;<br /> //不相等<br /> ?&gt;<br /> 2. 写出如下程序的输出结果<br /> <!--?<br /--> $a1 = null;<br /> $a2 = false;<br /> $a3 = 0;<br /> $a4 = &rdquo;;<br /> $a5 = &lsquo;0&prime;;<br /> $a6 = &lsquo;null&rsquo;;<br /> $a7 = array();<br /> $a8 = array(array());<br /> echo empty($a1) ? &lsquo;true&rsquo; : &lsquo;false&rsquo;;//true<br /> echo empty($a2) ? &lsquo;true&rsquo; : &lsquo;false&rsquo;;//true<br /> echo empty($a3) ? &lsquo;true&rsquo; : &lsquo;false&rsquo;;//true<br /> echo empty($a4) ? &lsquo;true&rsquo; : &lsquo;false&rsquo;;//true<br /> echo empty($a5) ? &lsquo;true&rsquo; : &lsquo;false&rsquo;;//true<br /> echo empty($a6) ? &lsquo;true&rsquo; : &lsquo;false&rsquo;;//false<br /> echo empty($a7) ? &lsquo;true&rsquo; : &lsquo;false&rsquo;;//true<br /> echo empty($a8) ? &lsquo;true&rsquo; : &lsquo;false&rsquo;;//false<br /> ?&gt;<br /> 3. 写出如下程序的输出结果<br /> <!--?<br /--> $test = &lsquo;aaaaaa&rsquo;;<br /> $abc = &amp; $test;<br /> unset($test);<br /> echo $abc;//&rsquo;aaaaaa&rsquo;<br /> ?&gt;<br /> 4. 写出如下程序的输出结果<br /> <!--?$count = 5;<br /--> function get_count(){<br /> static $count = 0;<br /> return $count++;<br /> }<br /> echo $count; //5<br /> ++$count;<br /> echo get_count();//0<br /> echo get_count();//1<br /> ?&gt;<br /> 5. 写出如下程序的输出结果<br /> <!--?<br /--> $GLOBALS['var1'] = 5;<br /> $var2 = 1;<br /> function get_value(){<br /> global $var2;<br /> $var1 = 0;<br /> return $var2++;<br /> }<br /> get_value();<br /> echo $var1; //0<br /> echo $var2; //2<br /> ?&gt;<br /> 6. 写出如下程序的输出结果<br /> <!--?<br /--> function get_arr($arr){<br /> unset($arr[0]);<br /> }<br /> $arr1 = array(1, 2);<br /> $arr2 = array(1, 2);<br /> get_arr(&amp;$arr1);<br /> get_arr($arr2);<br /> echo count($arr1);//1<br /> echo count($arr2);//2<br /> ?&gt;<br /> 同类</p>
RangeTime:0.007573s
RangeMem:219.59 KB
返回顶部 留言