试题总结(一)

 

strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown') //比较两个字符串是否相等,不区分大小写


 

1.写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名

例如: http://www.sina.com.cn/abc/de/fg.php?id=1 需要取出 php 或 .php

 

 function getExt($url){
    $arr = parse_url($url);
    print_r($arr);
   return strstr($arr['path'],'.');
 }

 

 2.二维数组排序

$data['one'] =  array('volume' => 67, 'edition' => 2);
$data['two'] =  array('volume' => 86, 'edition' => 1);
$data['three'] =  array('volume' => 85, 'edition' => 6);
$data[] =  array('volume' => 98, 'edition' => 2);
$data[] =  array('volume' => 86, 'edition' => 6);
$data[] =  array('volume' => 67, 'edition' => 7);

print_r( $data);

//  取得列的列表
foreach ( $data  as  $key =>  $row) {
     $volume[ $key]  =  $row['volume'];
     $edition[ $key] =  $row['edition'];
}

//  将数据根据 volume 降序排列,根据 edition 升序排列
// 把 $data 作为最后一个参数,以通用键排序

array_multisort( $volume, SORT_DESC,   $data);
print_r( $data);

// 自定义二维数组排序函数
function array_sort( $arr, $keys, $type='asc'){
     $keysvalue =  $new_array =  array();
     foreach ( $arr  as  $k=> $v){
         $keysvalue[ $k] =  $v[ $keys];
    }
     if( $type == 'asc'){
         asort( $keysvalue);
    } else{
         arsort( $keysvalue);
    }
     reset( $keysvalue);
     foreach ( $keysvalue  as  $k=> $v){
         $new_array[ $k] =  $arr[ $k];
    }
     return  $new_array;

 3:数据库sql语句类型

  • ddl:数据库定义语言 (create,alter,drop)
  • dml:数据库操作语言(增删改差)
  • dtl:数据库事务语句(commit ,rollback,savepoint)
  • dcl:数据库控制语句(revoke,grank)

 

4:使对象可以像数组一样进行foreach循环,要求属性必须是私有:

 

PHP5开始支持了接口, 并且内置了Iterator接口, 所以如果你定义了一个类,并实现了Iterator接口,那么你的这个类对象就是ZEND_ITER_OBJECT,否则就是ZEND_ITER_PLAIN_OBJECT.
对于ZEND_ITER_PLAIN_OBJECT的类,foreach会通过HASH_OF获取该对象的默认属性数组,然后对该数组进行foreach.
而对于ZEND_ITER_OBJECT的类对象,则会通过调用对象实现的Iterator接口相关函数来进行foreach.

 

 

class sample  implements Iterator
{
     private  $_items =  array(1,2,3,4,5,6,7);

     public  function __construct() {
                  ; // void
    }
     public  function  rewind() {  reset( $this->_items); }
     public  function  current() {  return  current( $this->_items); }
     public  function  key() {  return  key( $this->_items); }
     public  function  next() {  return  next( $this->_items); }
     public  function valid() {  return (  $this-> current() !==  false ); }
}

$sa =  new sample();
foreach( $sa  as  $key =>  $val){
     print  $key . "=>" . $val;
}

 

5.PHP的垃圾回收机制

php 5.3之前使用的垃圾回收机制是单纯的“引用计数”,也就是每个内存对象都分配一个计数器,当内存对象被变量引用时,计数器+1;当变量引用撤掉后,计数器-1;当计数器=0时,表明内存对象没有被使用,该内存对象则进行销毁,垃圾回收完成。

“引用计数”存在问题,就是当两个或多个对象互相引用形成环状后,内存对象的计数器则不会消减为0;这时候,这一组内存对象已经没用了,但是不能回收,从而导致内存泄露;

php5.3开始,使用了新的垃圾回收机制,在引用计数基础上,实现了一种复杂的算法,来检测内存对象中引用环的存在,以避免内存泄露。

 

 

1)垃圾回收的时机

Php中,引用计数为0,则内存立刻释放;也就是说,不存在环状引用的变量,离开变量的作用域,内存被立刻释放。

环状引用检测则是在满足一定条件下触发,所以在上面的例子中,会看到使用的内存有大幅度的波动;也可以通过 gc_collect_cycles 函数来主动进行环状引用检测。

2) &符号的影响

显式引用一个变量,会增加该内存的引用计数:

$a = "something";

$b = &$a;

此时unset($a), 但是仍有$b指向该内存区域的引用,内存不会释放。

3)unset函数的影响

unset只是断开一个变量到一块内存区域的连接,同时将该内存区域的引用计数-1;在上面的例子中,循环体内部,$a=new A(); unset($a);并不会将$a的引用计数减到零;

4)= null 操作的影响;

$a = null 是直接将$a 指向的数据结构置空,同时将其引用计数归0。

5)脚本执行结束的影响

脚本执行结束,该脚本中使用的所有内存都会被释放,不论是否有引用环。

 

 6:用php实现一个双向队列

 

deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。双向队列(双端队列)就像是一 个队列,但是你可以在任何一端添加或移除元素。而双端队列是一种数据结构,定义如下:

A deque is a data structure consisting of a list of items, on which the following operations are possible.

  • push(D,X) -- insert item X on the rear end of deque D.
  • pop(D) -- remove the front item from the deque D and return it.
  • inject(D,X) -- insert item X on the front end of deque D.
  • eject(D) -- remove the rear item from the deque D and return it.

Write routines to support the deque that take O(1) time per operation.

翻译:双端队列(deque)是由一些项的表组成的数据结构,对该数据结构可以进行下列操作:

  • push(D,X) 将项X 插入到双端队列D的前端
  • pop(D) 从双端队列D中删除前端项并将其返回
  • inject(D,X) 将项X插入到双端队列D的尾端
  • eject(D) 从双端队列D中删除尾端项并将其返回
 

 

class deque
{
     public  $queue  =  array();
     public  $length = 0;
   
     public  function frontAdd( $node){
         array_unshift( $this->queue, $node);
         $this->countqueue();
    }
     public  function frontRemove(){
         $node =  array_shift( $this->queue);
         $this->countqueue();
         return  $node;
    }
      
     public  function rearAdd( $node){
         array_push( $this->queue, $node);
         $this->countqueue();
    }
     
     public  function rearRemove(){
         $node =  array_pop( $this->queue);
         $this->countqueue();
         return  $node;
    }
     
     public  function countqueue(){
         $this->length =  count( $this->queue);    
    }
}
$fruit =  new deque();
echo  $fruit -> length;
$fruit -> frontAdd("Apple");
$fruit -> rearAdd("Watermelon");
echo '<pre>';
print_r( $fruit);
echo '</pre>';

 运行结果:

0
deque  Object
(
    [queue] =>  Array
        (
            [0] => Apple
            [1] => Watermelon
        )
    [length] => 2
)

 7:私有ip地址范围

   A: 10.0.0.0~10.255.255.255 /8
   C:192.168.0.0~192.168.255.255 /16
   B:172.16.0.0~172.31.255.255 /12

 

 8:计算两个文件的性对路径

$a = '/a/b/c/d/e.php';
$b = '/a/b/c.php';
/* *
 * 1.把每个路径用/分隔成数组
 * 2.把相同部分删除掉
 * 3.$arr_b数组的个数(..) 加上$arr_a就是$b相对于$a的相对路径了 
 
*/
function get_relative( $a$b) {

     // 分割成数组
     $arr_a =  explode ( '/',  $a);
     $arr_b =  explode ( '/',  $b);

     // 去除两个数字的公共部分,并且在把最后一个文件名去掉
     $arr_diff_a =  array_diff( $arr_a$arr_b);
     $arr_diff_b =  array_diff( $arr_b$arr_a);
     array_pop( $arr_diff_a);
     array_pop( $arr_diff_b);

     // 求数组b的长度
     $blen =  count( $arr_diff_b);

     // 拼接相对路径的字符串
     $str =  $blen ?  str_pad('',  $blen*3, '../') : './';
     $str .=  implode('/',  $arr_diff_a);

     echo  $str;
}
get_relative (  $a$b );

 9:动态表,静态表

  • 静态表是当一个表中没有采用varchar,blob,text这种可变长字段时,此表就是静态表,反之,如果一个表内存在至少一个可变长字段时,或者如果一个表被用ROW_FORMAT=DYNAMIC选项来创建,此表就为动态

10:获取文件扩展名

$str = "/www/a.class.php";
     function extend_1( $file_name)  
    {  
         $retval="";  
         $pt= strrpos( $file_name, ".");  
         if ( $pt$retval= substr( $file_name$pt+1);  
         return ( $retval);  
    }  

     function extend_2( $file_name)  
    {  
         $extend =  pathinfo( $file_name);  
         $extend =  strtolower( $extend["extension"]);  
         return  $extend;  
    }  

     function extend_3( $file_name)  
    {  
         $extend = explode("." ,  $file_name);  
         return  array_pop( $extend);  
    } 
    
      function extend_4( $file_name)  
    {  
         return  pathinfo( $file, PATHINFO_EXTENSION);  
    }  
    
    
     function extend_5( $file_name)  
    { 
         preg_match_all("/(?:\.\w+)$/",  $file_name$matches);
         print_r( $matches);  
    }  
     echo extend_5( $str);

 11:打印三角形

/* *
 *        *
         ***
        *****
       *******
      *********
     ***********
    *************
   ***************
  *****************
 *******************
 
*/
for( $i=0; $i<10; $i++)
{
     for( $j=0; $j<10- $i; $j++)
    {
         echo '&nbsp;';
    }

     for( $j=0; $j<2* $i+1; $j++)
    {
         echo '*';
    }
     echo '<br/>';
}

 12:杨辉三角(没有控制缩进)

/* *
 * 把每行的数据都放到一个二维数组里面,然后遍历数组 
 
*/
function yanghuisanjiao( $lines)
{
     $sc[0][0] = 1; // 第一行为1 
     for( $i=1; $i<= $lines; $i++)
    {
         for( $j=0; $j<= $i; $j++)
        {
             if( $j==0 ||  $j== $i){
                 $sc[ $i][ $j] = 1; // 每行的首尾都为1
            }  else{
                 $sc[ $i][ $j] =  $sc[ $i-1][ $j-1] +  $sc[ $i-1][ $j];
            }
        }
    }
    
     for( $i=0; $i< count( $sc); $i++)
    {
         // print_r($sc[count($sc)-1-$i]);
         for( $k=0; $k< count( $sc[ count( $sc)-1- $i]); $k++)
        {
             echo '&nbsp;';
        }
        
         for( $j=0; $j< count( $sc[ $i]); $j++)
        {
             echo  $sc[ $i][ $j].'&nbsp;';
        }
         echo '<br/>';
    }

}
yanghuisanjiao(10);

 

 13:

有mail.log的一个文档,内容为若干邮件地址,其中用'\n'将邮件地址分隔。要求从中挑选出sina.com的邮件地址(包括从文件读取、过滤到列印出来)。
mail.log内容如下:
james@sina.com.cn
jack@163.com
zhansan@sohu.com
lisi@hotmail.com
wangwu@gmail.com
aaaa@sina.com

(如果是大公司,往往还告诉你,该log日志的大小1G,而能供你使用的内存只有500M,让你综合考虑速度,内存来完成该题)

 

foreach( $list  as  $v) {
     if( substr( $v,-9) == '@sina.com') {
         echo  $v,'<br />';
    }
}


$fh =  fopen('mail.log','r') {
     while(! feof( $fh)) {
         $line =  fgets( $fh);
         if( substr( $line,-9) == '@sina.com') {
             echo  $v,'<br />';
         }
    }
}

 14:交换两个变量的位置

$a = 1;
$b = 2;

$a =  $a^ $b;
$b =  $a^ $b;
$a =  $a^ $b;

echo  $a;
echo  $b;

15: 邮箱正则

/^\w+([-+.]\w+)*@\w+([-+.]\w+)*\.\w+([-+.]\w+)*$/;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值