PHP面试题及答案(四)

1 请说明 PHP 中传值与传引用的区别。什么时候传值什么时候传引用?
   答: 传值只是把某一个变量的值传给了另一个变量,而引用则说明两者指向了同一个地方。
2 在PHP中error_reporting这个函数有什么作用?
   答: The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script.
3 请用正则表达式(Regular Expression)写一个函数验证电子邮件的格式是否正确。
答:

<?php
if(isset($_POST['action']) &&$_POST['action']=='submitted')
{
    
$email=$_POST['email'];
    
if(!preg_match("/^(?:w+.?)*w+@(?:w+.?)*w+$/",$email))
     {
        
echo"电子邮件检测失败";
     }
    
else
     {
        
echo"电子邮件检测成功";
     }
}
else
{
?>
<html>
<head><title>EMAIL检测</title>
<script type="text/javascript">
    
function checkEmail(sText)
     {
        
var reg=/^(?:w+.?)*w+@(?:w+.?)*w+$/;
        
var email=document.getElementById(sText).value;
        
if(!reg.test(email))
         {
             alert(
"电子邮件检测失败");
         }
        
else
         {
             alert(
"电子邮件格式正确");
         }
     }
</script>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
电子邮件:
<input type="text" id="email" name="email"/><br />
<input type="hidden" name="action" value="submitted"/>
<input type="button" name="button" value="客户端检测" onclick="checkEmail('email')"/>
<input type="submit" name="submit" value="服务器端检测"/>
</form>
</body>
</html>
<?php
}
?>

4 简述如何得到当前执行脚本路径,包括所得到参数。

<?php
echo"http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
//echo "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>

5 有一个一维数组,里面存储整形数据,请写一个函数,将他们按从大到小的顺序排列。要求执行效率高。并说明如何改善执行效率。(该函数必须自己实现,不能使用php函数)

 

<?php
function BubbleSort(&$arr)
{
    
$cnt=count($arr);
    
$flag=1;
    
for($i=0;$i<$cnt;$i++)
     {
        
if($flag==0)
         {
            
return;
         }
        
$flag=0;
        
for($j=0;$j<$cnt-$i-1;$j++)
         {
            
if($arr[$j]>$arr[$j+1])
             {
                
$tmp=$arr[$j];
                
$arr[$j]=$arr[$j+1];
                
$arr[$j+1]=$tmp;
                
$flag=1;
             }
         }
     }
}
$test=array(1,3,6,8,2,7);
BubbleSort(
$test);
var_dump($test);
?>

 

6 请举例说明在你的开发过程中用什么方法来加快页面的加载速度
   答:要用到服务器资源时才打开,及时关闭服务器资源,数据库添加索引,页面可生成静态,图片等大文件单独服务器。使用代码优化工具啦

posted @ 2013-04-24 16:27  keethebest  阅读(246)  评论(0编辑  收藏  举报