[字号:  ]

【公司面试题之:腾讯PHP工程师笔试题】

发布时间:2008-10-05 22:06   作者: blankyao   信息来源: PHPChina 开源社区门户
1. 请对POSIX风格和兼容Perl风格两种正则表达式的主要函数进行类比说明
ereg preg_match
ereg_replace preg_replace


2. 请说明在php.ini中safe_mode开启之后对于PHP系统函数的影响


3. PHP5中魔术方法函数有哪几个,请举例说明各自的用法

__sleep
__wakeup
__toString
__set_state
__construct,
__destruct
__call,
__get,
__set,
__isset,
__unset
__sleep,
__wakeup,
__toString,
__set_state,
__clone
__autoload


4. 请写出让,并说明如何在命令行下运行PHP脚本(写出两种方式)同时向PHP脚本传递参数?


5. PHP的垃圾收集机制是怎样的


6.使对象可以像数组一样进行foreach循环,要求属性必须是私有。
(Iterator模式的PHP5实现,写一类实现Iterator接口)


7.请写一段PHP代码,确保多个进程同时写入同一个文件成功


8. 用PHP实现一个双向队列


9. 使用正则表达式提取一段标识语言(html或xml)代码段中指定标签的指定属性值(需考虑属性值对不规则的情况,如大小写不敏感,属性名值与等号间有空格等)。此处假设需提取test标签的attr属性值,请自行构建包含该标签的串

<test attr=”ddd”>

<test attr\s*=\s*[“ ¦’](.*?)[” ¦’].*?>


10.请使用socket相关函数(非curl)实现如下功能:构造一个post请求,发送到指定http server的指定端口的指定请求路径(如http://www.example.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)) {
echo fgets($fp, 128);
}
fclose($fp);
}

$url = ‘http://www.example.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.脚本执行时间,启用xdebug,使用WinCacheGrind分析。
2.数据库查询,mysql使用EXPLAIN分析查询,启用slow query log记录慢查询。


PHP LAMP Engineer Test Paper
Question 1
What does <? echo count ("123") ?> print out?
A) 3
B) False
C) Null
D) 1
E) 0

Question 2
Which of the following snippets prints a representation of 42 with two decimal places?
A) printf("%.2d\n", 42);
B) printf("%1.2f\n", 42);
C) printf("%1.2u\n", 42);

Question 3
Given
$text = 'Content-Type: text/xml';
Which of the following prints 'text/xml'?
A) print substr($text, strchr($text, ':'));
B) print substr($text, strchr($text, ':') + 1);
C) print substr($text, strpos($text, ':') + 1);
D) print substr($text, strpos($text, ':') + 2);
E) print substr($text, 0, strchr($text, ':')
Question 4
What is the value of $a?
<?php
$a = in_array('01', array('1')) == var_dump('01' == 1);
?>
A) True
B) False
Question 5
What is the value of $result in the following PHP code?
<?php
function timesTwo($int) {
$int = $int * 2;
}
$int = 2;
$result = timesTwo($int);
?>;
Answer: NULL
Question 6
The code below ___________ because ____________.
<?php
class Foo {
?>
<?php
function 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 7
When turned on, ____________ will _________ your script with different variables from HTML forms and cookies.
A) show_errors, enable
B) show_errors, show
C) register_globals, enhance
D) register_globals, inject
Question 8
What will be the output of the following PHP code:
<?php
echo count(strlen("http://php.net"));
?>
Answer: 1
Question 9
What is the best all-purpose way of comparing two strings?
A) Using the strpos function
B) Using the == operator
C) Using strcasecmp()
D) Using strcmp()
Question 10
What is the difference between "print()" and "echo()"?
Answer: print is a function,echo is a language construct

原帖地址http://www.phpcup.cn/thread-352-1-1.html
更多面试题
http://www.blankyao.cn/blog/php-jobs.html

最新回复

lxylxy888666 at 2008-10-05 23:09:17
好难啊
blankyao at 2008-10-06 07:01:10
都是由点变态的基础题,呵呵
ilsanbao at 2008-10-07 09:37:39
很基础,很强大~
letmepass at 2008-10-07 10:01:14
期待答案ING~~~~~~
凌雪 at 2008-10-07 10:31:50
啥都木看明白
我似乎不知道自己学没学过PHP了
deeka at 2008-10-07 14:05:57
考研的试题吧?
naodai at 2008-10-07 16:46:52
恩,期待答案!
mataihang at 2008-10-07 17:32:23
题目不是特别难 大部分都是书上的

只是非常少用到 至少常规开发中

如果没有非常充分准备 在没资料可查 基本就PASS了
海边的水手 at 2008-10-07 18:20:24
按100分算,我的得分不会超过10分@@@
Sunyanzi at 2008-10-08 08:48:06
题目不难 ... 除了第五题之外都很容易能答出来 ...

关于第五题 ... php 有垃圾收集机制么 ...?

不是单线程执行完成之后就释放一切资源么 ...

未执行完成的时候 ... 保存的东西如果不手动 unset 就会一直留在内存里 ...

Java 有个 GC 可以搞定这一切 ... php 这边真的有垃圾收集这个概念么 ...?

感觉出这个题的人是 Java 的精通者 ... 不太像个 php 大师的样子 ...
pylong at 2008-10-08 09:30:16
每门语言都有垃圾收集机制的

[ 本帖最后由 pylong 于 2008-10-8 10:02 编辑 ]
d9tx at 2008-10-08 10:01:01
有些难度的题,
目测我只能回答一半不到...
wodoe at 2008-10-08 10:19:01
还英文啊?看不懂 给100分我拿1分 啥也不会昵
Sunyanzi at 2008-10-08 22:48:31

QUOTE:

原帖由 pylong 于 2008-10-8 09:30 发表
每门语言都有垃圾收集机制的
那么 ... 我对 php 的垃圾收集机制的了解就是 ...

使用类似 instance = null / unset / mysql_free_result / mysql_close 这样的命令手动完成 ...

不知道还有其他的什么没有呢 ..?
peacock at 2008-10-08 23:51:19
不开卷(至少也得给个参考手册),谁记得了这么多啊
pylong at 2008-10-09 11:41:32
http://www.hudzilla.org/phpbook/read.php/18_1_10
之前无意间看到的

不过说得还不清楚,比如如何回收
比如python,它是有个垃圾回收器进行回收的

[ 本帖最后由 pylong 于 2008-10-9 11:47 编辑 ]
kineyj at 2008-10-09 18:38:00
一个都不会做, 我实在是太菜了
seewalking at 2008-10-19 00:19:31
能做到一大半啦!确实这个题目还是不好做的啦!
ginux at 2008-10-19 00:43:12
不难 但是很...
biyao at 2008-10-19 15:29:47
真的是考死人,好像真的像7楼说的,真的没学过PHP一样