• php面试题


    网上找的比较好的腾讯php面试题,大部分答案已经整理出来,特在php教程中贴出供大家参考!
    
    1 . 请对 POSIX 风格和兼容 Perl 风格两种正则 表达式的主要函数进行类比说明
    
    ereg
        
    
    preg_match
    
    ereg_replace
        
    
    preg_replace
    
    2 . 请说明在 php .ini 中 safe_mode 开启之后对于 PHP 系统 函数的影响
    
    开启之后,主要会对系统操作、文件、权限设置等方法产生影响,平常项目基本上也用不到这些方法。主要我想还是用来应对webshell吧,减少被人植入webshell所带来的某些安全问题。
    
    3 .PHP5 中魔术 方法 函数有哪几个,请举例说明各自的用法
    
    __sleep
    
    __wakeup
    
    __toString
    
    __set_state
    
    __construct,
    
    __destruct
    
    __call,
    
    __get,
    
    __set,
    
    __isset,
    
    __unset
    
    __sleep,
    
    __wakeup,
    
    __toString,
    
    __set_state,
    
    __clone
    
    __autoload
    
    4 . 请写出让,并说明如何在命令行下运行 PHP 脚本(写出两种方式)同时向 PHP 脚本传递参数?
    
    1. Php filename.php $agr1 $agr2
    
    2. php –r “<?php code?>5 . PHP 的垃圾收集机制是怎样的
    
    PHP作为脚本语言是页面结束即释放变量所占内存的。 当一个 PHP线程结束时,当前占用的所有内存空间都会被销毁,当前程序中所有对象同时被销毁。GC进程一般都跟着每起一个SESSION而开始运行的.gc目的是为了在session文件过期以后自动销毁删除这些文件. 在PHP中,没有任何变量指向这个对象时,这个对象就成为垃圾。PHP会将其在内存中销毁;这是PHP 的GC垃圾处理机制,防止内存溢出。 执行这些函数也可以起到回收作用 __destruct /unset/mysql_close /fclose php对session有明确的gc处理时间设定 session.gc_maxlifetime 如果说有垃圾,那就是整体的程序在框架使用中,会多次调用同一文件等等造成的非单件模式等。所以在出来的时候,必要的用_once 引用,在声明类的时候使用单件模式。还有简化逻辑等等。而如果妄想让PHP自己本身管理内存,进行垃圾管理。呵呵。好像PHP还办不到,对于析构函数,ANDI在他的书里写的很明白。可有可无,不可置否。而内存管理的东西一般都是桌面程序更多去考虑的。
    
    6 .使对象可以像数组一样进行 foreach 循环,要求属性必须是私有。
    
    (Iterator 模式的 PHP5 实现,写一类实现 Iterator 接口 )
    
    <?php
    
    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;
    
    }
    
    ?>
    
     
    
    7 .请写一段 PHP 代码 ,确保多个进程同时写入同一个文件 成功
    
    function write_file($filename, $content)
    {
        $lock = $filename . '.lck';
        $write_length = 0;
        while(true) {
            if( file_exists($lock) ) {
                usleep(100);
            } else {
                touch($lock);
                $write_length = file_put_contents($filename, $content, FILE_APPEND);
                break;
            }
        }
        if( file_exists($lock) ) {
            unlink($lock);
        }
        return $write_length;
    }
    
    
    8 . 用 PHP 实现一个双向队列
    
    class DEQueue {
        //存储
        protected $_storage = array();
        
        //入头
        public function unshift($element)
        {
            return array_unshift($this->_storage, $element);
        }
        
        //入尾
        public function push($element)
        {
            return array_push($this->_storage, $element);
        }
        
        //出尾
        public function pop()
        {
            return array_pop($this->_storage);
        }
        
        //出头
        public function shift()
        {
            return array_shift($this->_storage);
        }
        
        //长度
        public function length()
        {
            return count($this->_storage);
        }
    }
    
    
    9 .使用正则表达式提取一段标识语言( html 或 xml )代码段中指定标签的指定属性值(需考虑属性值对不规则的情况,如大小写不敏感,属性名值与等号间有 空格等)。此处假设需提取 test 标签的 attr 属性值,请自行构建包含该标签的串
    
    <test attr=”ddd”>
    
    <test attr/s*=/s*[“|’](.*?)[”|’].*?>
    
    10 .请使用 socket 相关函数(非 curl )实现如下功 能:构造一个 post 请求,发送到指定 http server 的指定端口的指定请求路径(如 http://www.phpddt.com:8080/test )。请求中包含以下变量:
    
    用户名( username ):温柔一刀
    
    密码( pwd ): &123=321&321=123&
    
    个人简介( intro ): Hello world !
    
    且该 http server 需要以下 cookie 来进行简 单的用户动作跟踪:
    
    cur_query : you&me
    
    last_tm : ... (上次请求的 unix 时间戳,定为当前请求时间前 10 分钟)
    
    cur_tm : ... (当前请求的 unix 时间戳)
    
    设置超时为 10 秒,发出请求后, 将 http server 的响应内容输出。
    
    Function encode($data, $sep = ‘&’){
    
    while (list($k,$v) = each($data)) {
    
    $encoded .= ($encoded ? "$sep" : "");
    
    $encoded .= rawurlencode($k)."=".rawurlencode($v);
    
    }
    
    Return $encoded;
    
    }
    
    Function post($url, $post, $cookie){
    
    $url = parse_url($url);
    
    $post = encode($data, ‘&’);
    
    $cookie = encode($cookieArray, ‘;’);
    
    $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80,$errno,$errstr,10);
    
    if (!$fp) return "Failed to open socket to $url[host]";
    
    fputs($fp, sprintf("POST %s%s%s HTTP/1.0/n", $url['path'], $url['query'] ? "?" : "", $url['query']));
    
    fputs($fp, "Host: $url[host]/n");
    
    fputs($fp, "Content-type: application/x-www-form-urlencoded/n");
    
    fputs($fp, "Content-length: " . strlen($encoded) . "/n");
    
    fputs($fp, "Cookie: $cookie/n/n");
    
    fputs($fp, "Connection: close/n/n");
    
    fputs($fp, "$post /n");
    
    
       while (!feof($fp)) {
           echofgets($fp,128);
       }
       fclose($fp);
    
    }
    
    $url = ‘http://www.phpddt.com:8080/test ’;
    
    $encoded = username= 温柔一刀 & pwd=
    
    $post = array(
    
    ‘ username ’ => ‘温柔一刀’ ,
    
    ‘ pwd => ‘&123=321&321=123&’,
    
    ‘ intro => ‘Hello world!’
    
    );
    
    $cookie = array(
    
    ‘ cur_query’ => ‘ you&me,
    
    ‘ last_tm’ =>time() -600,
    
    ‘cur_tm ‘=> time()
    
    );
    
    Post($url, $post, $cookie);
    
    11 .你用什么方法检查 PHP 脚本的执行效率(通常是脚本执行时间)和数据库 SQL 的效率(通常是数据库 Query 时间), 并定位和分析脚本执行和数据库查询的瓶颈所在?
    
    1.PHP执行时间:
    
    $begin=microtime(true); //获取程序开始执行的时间
    // some code here 待执行的代码
    $stop=microtime(true); //获取程序执行结束的时间
    list($m0,$s0)=explode(" ",$begin);
    list($m1,$s1)=explode(" ",$stop);
    $runtime=($s1+$m1-$s0-$m0)*1000;
    echo '<br />当前脚本执行时间:'.$etime-$stime.'微秒'; 
    
    2.SQL执行时间(其实和上面一样):
    
    $begin=microtime();
    mysql_query($sql);
    $stop=microtime();
    list($m0,$s0)=explode(" ",$begin);
    list($m1,$s1)=explode(" ",$stop);
    $runtime=round(($s1+$m1-$s0-$m0)*1000,4);
    echo '<br />当前脚本执行时间:'.$runtime.'ms';
  • 相关阅读:
    实验8 SQLite数据库操作
    实验7 BindService模拟通信
    实验6 在应用程序中播放音频和视频
    实验6 在应用程序中播放音频和视频
    实验5 数独游戏界面设计
    实验4 颜色、字符串资源的使用
    实验五存储管理实验
    实验四:主存空间的分配和回收
    实验三 进程调度模拟程序
    实验8 SQLite数据库操作
  • 原文地址:https://www.cnblogs.com/fyy-888/p/5324010.html
Copyright © 2020-2023  润新知