腾讯PHP工程师笔试题.docx
1 .请对POSIX风格和兼容Perl风格两种正那么表达式的主要函数进行类比说明ereg preg_matchereg_replace preg_replace.请说明在php.ini中safe_mode开启之后对于PHP系统函数的影响2 . PHP5中魔术方法函数有哪几个,请举例说明各自的用法sleepwakeuptoStringset_stateconstruct,destructcall,get,set,isset,unsetcloneautoloadreturn each($this->property);/测试代码class test var $varl = 111;var $var2 = 12*;var $var3 = 13 T;var $var4 = 14 T;function test () )$obj = new test; foreach ($obj as $k=>$v) echo n$k => $vnn;)$itrator = new ObjIterator($obj);$iterator->reset();while ($s = $iterator->each () print_r($s);)7.多进程写文件,参考这个帖子: :/ = 344 通过php中的pcnl_*函数来生成多进程操作同一个文件<?phpfunction writefile($fileA $pid) static $count;$count+;echo "call writefile, pid=$pid, count=$countnn ; while (1) $fp = fopen($file, na+"); if(!$fp)/sleep(rand(100,1000); continue;) break;)while(1)if ( !flock($fp, LOCK_EX) /usleep(rand(100z1000); /usleep (1000);continue;fwrite($fp, "PHD: $pidtTest contents".$count . ”n");flock($fp, LOCK_UN);fclose ($fp); break; return;)echo "Start .nn;unlink(1 ./fork_test3.txt *);for($i=0; $i<5; $i+)$pid = pcntl_fork();switch($pid)case -1: die(* fork error *);break;case 0 : writefile(n./fork_test3.txt"r posix_getpid();break; default: pcntl_wait($status);exit;).双向队列,参考这个帖子: :/ = 366<?php/Input limit double-ende queueclass DoubleEndedQueuel var $queue = array ();function add($var) return array_push($this->queue, $var);function frontRmov() return array_shift($this->queue); ) function rearRemove()return array_pop($this->queue); )/Output limit double-ende queueclass DoubleEndedQueue2 var $queue = array();function remove() return array_pop($this->queue);) function frontAdd($var)return array_unshift($this->queue, $var); function rarAdd($var)return array_push($this->queue, $var);)/Test cod$q = new DoubleEndedQueuel;$q->add(1aaa *);$q->add(1bbb1);$q-add(!cccc * );$q->add(1dddd *);echo $q->frontRemove();echo "nn;echo $q->rearRemove();print_r($q);.已回答,附带两个参考的:<tests+attrs*=s*n(AH+)n>或 <tests+attrs*=s*"(A,,|A>)+)">8 .题目已经实现,也可参考: :/ = 231.个人发挥4 .请写出让,并说明如何在命令行下运行PHP脚本(写出两种方式)同时向PHP脚本传递参数?5 . PHP的垃圾收集机制是怎样的.使对象可以像数组一样进行foreach循环,要求属性必须是私有。(Iterator模式的PHP5实现,写一类实现iterator接口).请写一段PHP代码,确保多个进程同时写入同一个文件成功.用PHP实现一个双向队列.使用正那么表达式提取一段标识语言(html或xml)代码段中指定标签的指定属性值(需考虑属性值 对不规那么的情况,如大小写不敏感,属性名值与等号间有空格等)。此处假设需提取test标签的at"属 性值,请自行构建包含该标签的串<test attr="ddd"><test attrs*=s*u 门(.*?)" ;'.*?>.请使用socket相关函数(非curl)实现如下功能:构造一个post请求,发送到指定 server的指定端口的指定请求路径(如 : example :8080/test)。请求中包含以下变量:用户名(username):温柔一刀密码(pwd) : &123 = 321&321 = 123&个人简介(intro) : Hello world!且该 server需要以下cookie来进行简单的用户动作跟踪:cur_query: you&melast_tm: .(上次请求的unix时间戳,定为当前请求时间前10分钟)cur_tm:(当前请求的unix时间戳)设置超时为10秒,发出请求后,将 server的响应内容输出。1 .<font face=nCourier Newn>Function encode($data, $sep =‘&’)2 .while (list($kz $v) = each($data) 3 . $encoded .= ($encoded ? n$sepn :4 . $encoded . = rawurlencode ($k) ., = n . rawurlencode ($v);5 . 6 . Return $encoded;7 . 8 .9 .Function post ($url, $post, $cookie) 10 . $url = parse_url($url);11 . $post = encode($data, ‘&’);12 . $cookie = encode($cookieArrayz Slsquo;Srsquo;);$errnoz$errnoz$errnoz13 . $fp = fsockopen($url1 host1, $url* port1 ? $urlfport1 : 80, $errstr, 10);14 . if ( !$fp) return "Failed to open socket to $urlLhostJ n;15 .16 . fputs($fp, sprintf("POST %s%s%s /l.0n”, $urlfpathf,$url1 query T ? "?n :$url* query 1);17 . fputs ($fp, nHost: $urlhostn");18 . fputs($fp, "Content-type: application/x-www-form-urlencodedn19. fputs($fp, nContent-length: ” . strlen($encoded) . nnn);20 . fputs($fp, nCookie: $cookiennn);21 . fputs($fp, "Connection: closenn”);22 . fputs($fp, n$post nn);23.24 . while (!feof ($fp) 25 . echo fgets($fp, 128);26. 27. . fclose($fp);28. )29. 30 . $url = ‘<a href=1 :/ example :8080/test1 target=_blank> :/ example :8080/test</a>’31 . $encoded = username=7J& pwd=32 . $post = array(33 . ‘ usernamesrsquo; => ‘温柔一刀&rsqu。;,34 . ‘pwd => ‘&123=321&321 = 123&’,35 . Slsquo;intro => ‘Hello world!Srsquo;36 .);37 . $cookie = array(38 . ‘cur_query’ => ‘you&me,39 . ‘last_tm’ => time() - 600,40 . ‘cur_tm ‘=> time()41 .);42 .43 . Post($url, $post, $cookie); </font>44 . ?>11 .你用什么方法检查PHP脚本的执行效率(通常是脚本执行时间)和数据库SQL的效率(通常是数据 库Query时间),并定位和分析脚本执行和数据库查询的瓶颈所在?1 .脚本执行时间,启用xdebug,使用WinCacheGrind分析。2 .数据库查询,mysql使用EXPLAIN分析查询,启用slow query log记录慢查询。PHP LAMP Engineer Test PaperQuestion 1What does <? echo count (n123") ?> print out?A) 3FalseNull1E)0Question 2Which of the following snippets prints a representation of 42 with two decimal places?A) printf("%,2dn"z 42);B) printf("%1.2fn"z 42);C) printf(“%l,2un”,42);Question 3Given$text = 'Content-Type: text/xml'Which of the following prints 'text/xml1?A) print substr($text, strchr($text,print substr($textz strchr($textz+ 1);print substr($text, strpos($text,+ 1);print substr($text, strpos($text, + 2);B) print substr($text, 0, strchr($text,Question 4What is the value of $a?<?php$a = in_array('01 array('l') = var_dump('01' = 1);?>TrueA) FalseQuestion 5What is the value of $result in the following PHP code?<?phpfunction timesTwo($int) $int = $int * 2;$int = 2;$result = timesTwo($int);?>;Answer: NULLQuestion 6The code below because.<?phpclass Foo ?> <?phpfunction bar() print "bar"?>A) will work, class definitions can be split up into multiple PHP blocks.B) will not work, class definitions must be in a single PHP block.C) will not work, class definitions must be in a single file but can be in multiple PHP blocks.D) will work, class definitions can be split up into multiple files and multiple PHP blocks. Question 7When turned on,will your script with different variables fromHTML forms and cookies.A) show_errors, enableshow_errors, showregister_globals, enhanceregister_globalsz injectQuestion 8What will be the output of the following PHP code:<?phpecho count(strlen("");?>Answer: 1Question 9What is the best all-purpose way of comparing two strings?A) Using the strpos functionUsing the = operatorUsing strcasecmp()Using strcmp()Question 10What is the difference between "print。” and "echo()"?Answer: print is a function,echo is a language construct附文章源产地管理员的答案:附:1 .看手册.【PHP安全几那么tips】.输入输出安全关闭register_global选项(php4及以前),初始化所有变量,能防止调用未定义变量 notice错误,也能够防范一些hacker行为接收任何变量的时候,务必记住:所有用户输入都是不安全的!如果确定用户输入数据 是整数(比方ID之类),可以使用(int)或intvalO函数强制类型转换,如果是字符串类型, 使用addslashes或mysql_escape_string(如果考虑数据库编码请连接数据库后使用 mysql_real_escape_string更安全,针对GBK等编码,使用该函数能够防范目前很多GBK 编码爆出的注入漏洞)注1(3)输出数据的时候,如果害怕html,可以使用strip_tags全部过滤,或者使用 htmlspecialchars进行html标签转义保证输出到页面不会构成威胁,另外,最好输入或者 输出的时候过滤所有的scriptXiframeXobject等标记和内容,有时候style或作为属 性的style也最好过滤一下注1:问题来自多字节编码。例如在GBK里,0xbf27并不是一个合法的双字节字符,因此 addslash()会把它转义成Oxbf5c27,碰巧Oxbf5c是一个合法的双字节字符,由此可以注入 一个 0x27 (') oaddslash ()和 mysql_escape_string 无药可救。mysql_real_escape_string() 可以根据字符集正确地转义,但是需要在建立数据库联接的时候指明“SET CHARACTER SET 'GBK' "。1 . php系统安全(1)翻开safe_mode是最重要的,同时设置open basedir,safe mode include dir, safe mode exec dir 等选项(2)当要操作或者包含文件的时候,使用realpath和basename检查文件是否是本地文件 (3)如果很多危险函数如果不使用,建议关闭,关闭函数修改disbale_functions选项,比如很多命令执行函数、eval、phpinfo等函数(4)如果不需要上传文件功能可以关闭,如果需要记得设置post_max_size选项为你合适的 大小,否那么文件系统很容易被塞满,并且move_uploaded_file来操作上传的文件,而且不 是使用copy把库文件(.inc)和数据文件(数据信息,配置信息,sqlite数据库等)不要与web目录放在 一起,防止被下载.看手册2 .头上写 #!/usr/bin/php 或者 /usr/bin/php xxxx.php.写时复制,引用为0时回收,参考这几个帖子: :/ = 342&extra = page%3Dl :/ = 288 :/ = 289.迭代器设计模式的实现,参考这个帖子: :/ = 365<?phpclass Obj工trator(var $property = array();function ObjIterator ($obj) if (is_object($obj)$this->property = get_obj ect_vars($obj);)function reset () rst($this-proprty);)function end()end($this->property);)function current () return current($this->property);function next () return next($this-property);)function prev()return prev($this->property);)