博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

php 面试题收集

Posted on 2011-09-28 14:56  bug yang  阅读(390)  评论(0编辑  收藏  举报

1、对于大流量的网站,您采用什么样的方法来解决访问量问题?(4分)
  首先,确认服务器硬件是否足够支持当前的流量
  其次,优化数据库访问。
  第三,禁止外部的盗链。
  第四,控制大文件的下载。
  第五,使用不同主机分流主要流量
  第六,使用流量分析统计软件。

 

附加答案: 
1 有效使用缓存,增加缓存命中率 
2 使用负载均衡 
3 对静态文件使用CDN进行存储和加速 
4 想法减少数据库的使用 
5 查看出现统计的瓶颈在哪里

2、写一个遍历文件夹所有文件的函数

 

function getFile($path)
{
if(is_dir($path))
{
$dir=opendir($path);
if($dir)
{
while($file=readdir($dir))
{
if($file=='.'||($file=='..'))
continue;
$newPath=$path.'/'.$file;
if(is_dir($newPath))
{
getFile($newPath);
}
else
{
echo $file.'<br/>';
}
}
}
else
{
echo 'can not open the dir ';
}
}
else
{
echo 'there is not a dir';
}

}
getFile('D:\ME\Music');

 3、写一个函数,算出两个文件的相对路径
  如 $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);

4、不用新变量直接交换现有两个变理的值

 

解法一、
<?php
$a=1234;
$b=4321;
list($a,$b)=array($b,$a);
?>
解法二、
$a和$b都为数值的时候:$a=$a+$b;$b=$a-$b;$a=$a-$b;

5、排序id为4,1,9,11的数据

select *
from temp
where id in(4,1,9,11)
order by field (id,4,1,11,9);

6、php的垃圾回收机制

  PHP 有一个非常简单的垃圾收集器,它实际上将对不再位于内存范围(scope)中的对象进行垃圾收集。垃圾收集的内部方式是使用一个引用计数器,因此当计数器达到 0 时(意味着对该对象的引用都不可用),对象将被当作垃圾收集并从内存中删除。

  __destruct /unset
  __destruct() 析构函数,是在垃圾对象被回收时执行。
  unset 销毁的是指向对象的变量,而不是这个对象。
7、php中的iterator用法:

 

<?php
class Sample implements Iterator{
private $items=array("a"=>"hello","b"=>"world","c"=>"ok");

public function current () {
return current($this->items);
}

public function next () {
return next($this->items);
}

public function key () {
return key($this->items);
}

public function valid () {
return ($this->current()!==false);
}

public function rewind () {
return reset($this->items);
}
}

$sample=new Sample();
foreach($sample as $key=>$value){
echo $key."=>".$value;
echo "<br/>";
}
output result:
a=>hello
b=>world
c=>ok

8、web开发为什么要声称静态页,php如何生成静态页?

  首先从访问速度上来说打开更快,对于保护网站数据库更加安全,更加让人期待的是搜索引擎太偏心于HTML页面了。

9、php正则表达式
10、如何修改SESSION的生存时间

 

<?php 
// 保存一天
$lifeTime = 24 * 3600;
session_set_cookie_params($lifeTime);
session_start();
$_SESSION["admin"] = true;
?>

 

额外题目:

http://www.phpweblog.net/jarryyip/archive/2008/05/04/3263.html

http://blog.sina.com.cn/s/blog_64e2219d0100xdk1.html

http://blog.csdn.net/v_july_v/article/details/6855788
http://bbs.yingjiesheng.com/thread-245284-1-1.html