I'm twiting

访问统计

free counters

PHP Interview questions from YAHOO

1. Which of the following will not add john to the users array? B
A. $users[] = ‘john’;
B. array_add($users,’john’);
C. array_push($users,‘john’);
D. $users ||= ‘john’;

2. What’s the difference between sort(), assort() and ksort? Under what circumstances would you use each of these?

sort() asort() ksort()
排序对象 元素的值 元素的值 元素键值
排序方式 字母顺序 字母顺序 字母顺序
重置索引 重置 不重置 不重置

3. What would the following code print to the browser? Why?
$num = 10;
function multiply(){
$num = $num * 10;
}
multiply();
echo $num;

由于foo()沒有指定 $num 为全局变量,所以 $num 的值是 10。

4. What is the difference between a reference and a regular variable? How do you pass by reference & why would you want to?

普通变量是直接传值
指针变量传递地址
引用变量是隐式传地址,如void f(int &x),实际传递的是x的地址p,但是函数内部使用x时会自动变成*p
void f(int &x) {x++}; 会变成
void f(int *p) {(*p)++};

5. What functions can you use to add library code to the currently running script?

6. What is the difference between foo() & @foo()?

foo() 會執行這個函式,任何解譯錯誤、語法錯誤、執行錯誤都會在頁面上顯示出來。
@foo() 在執行這個函式時,會隱藏所有上述的錯誤訊息。
很多應用程式都使用 @mysql_connect() 和 @mysql_query 來隱藏 mysql 的錯誤訊息,我認為這是很嚴重的失誤,因為錯誤不該被隱藏,你必須妥善處理它們,可能的話解決它們。

7. How do you debug a PHP application?

打印输出和写日志

8. What does === do? What’s an example of something that will give true for ‘==’, but not ‘===’?

「===」是給既可以送回布爾值「假」,也可以送回一個不是布爾值但卻可以賦與「假」值的函式,strpos() 和 strrpos() 便是其中兩個例子。
問題的第二部份有點困難,想一個「==」是假,但是「===」是真的例子卻很容易,相反的例子卻很少。但我終於找到以下的例子:
if (strpos(”abc”, “a”) == true)
{
// 這部分永不會被執行,因為 “a” 的位置是 0,換算成布爾值「假」
}
if (strpos(”abc”, “a”) === true)
{
// 這部份會被執行,因為「===」保證函式 strpos() 的送回值不會換算成布爾值.
}

9. How would you declare a class named “myclass” with no methods or properties?

class myclass
{
}

10. How would you create an object, which is an instance of “myclass”?

$obj =& new myclass();

11. How do you access and set properties of a class from within the class?

class myclass()
{
private $foo;
private function setFoo($foo)
{
$this->foo = $foo;
}

private function getFoo($foo)
{
return $this->foo;
}
}

12. What is the difference between include & include_once? include & require?

include include_once require require_once
报警方式 warning warning fatal error fatal error
文件不存在 继续执行 继续执行 停止执行 停止执行
重复包含 yes no yes no

13. What function would you use to redirect the browser to a new page? B
A. redir();
B. header();
C. location();
D. redirect();

14. What function can you use to open a file for reading and writing? C
A. fget();
B. file_open();
C. fopen();
D. open_file();

15. What’s the difference between mysql_fetch_row() and mysql_fetch_array()?

mysql_fetch_row mysql_fetch_assoc mysql_fetch_array mysql_fetch_object
return format numerical associative numerical and associative object

16. What does the following code do? Explain what’s going on there.
$date=’08/26/2003’;
print ereg_replace(“([0-9]+)/([0-9]+)/([0-9]+)”,\\2/\\1/\\3,$date);

The regular expansion match 08, 26 and 2003 then switch their position as 26, 08 and 2003.

17. Given a line of text $string, how would you write a regular expression to strip all the HTML tags from it?

/<(\/\s*)?((\w+:)?\w+)(\w+(\s*=\s*((["'])(\\["'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>/ig

18. What’s the difference between the way PHP and Perl distinguish between arrays and hashes?

— Perl hashes — Php hashes
%h = ();

%h = ( 'x' => 'y',
       'z' => 'w',
     );

$h{'x'} = 7;

while (($key,$value) = each(%h))
{ .. }

$a = keys(%h);
$b = values(%h);

delete $h{'x'};
$h = array();

$h = array( 'x' => 'y',
            'z' => 'w',
          );

$h['x'] = 7;

foreach ($h as $key => $value)
{ .. }

$a = array_keys($h);
$b = array_values($h);

unset( $h['x'] );

19. How can you get round the stateless nature of HTTP using PHP?

session cookie

20. What does the GD library do?

21. Name a few ways to output (print) a block of HTML code in PHP?
$foo = < <
here is html block.
EOF;
echo $foo;

22. Is PHP better than Perl? – Discuss.

考官现场提问:

1. 工作经历,工作职责,上一家公司的情况(女考官)。

2. 使用PHP多长时间?精通哪一块?

3. 使用MySQL多长时间?精通哪一块?

4. 比较MySQL的两种常用引擎的区别(MyIsAm和InnoDB)。
1.InnoDB不支持FULLTEXT类型的索引。
2.InnoDB 中不保存表的具体行数,也就是说,执行select count(*) from table时,InnoDB要扫描一遍整个表来计算有多少行,但是MyISAM只要简单的读出保存好的行数即可。注意的是,当count(*)语句包含 where条件时,两种表的操作是一样的。
3.对于AUTO_INCREMENT类型的字段,InnoDB中必须包含只有该字段的索引,但是在MyISAM表中,可以和其他字段一起建立联合索引。
4.DELETE FROM table时,InnoDB不会重新建立表,而是一行一行的删除。
5.LOAD TABLE FROM MASTER操作对InnoDB是不起作用的,解决方法是首先把InnoDB表改成MyISAM表,导入数据后再改成InnoDB表,但是对于使用的额外的InnoDB特性(例如外键)的表不适用。

另外,InnoDB表的行锁也不是绝对的,如果在执行一个SQL语句时MySQL不能确定要扫描的范围,InnoDB表同样会锁全表,例如update table set num=1 where name like “%aaa%”

任何一种表都不是万能的,只用恰当的针对业务类型来选择合适的表类型,才能最大的发挥MySQL的性能优势。

5. 从各种角度优化以下SQL语句:SELECT * FROM tablename WHERE id IN (13, 15, 18, 19) and age=21 ORDER BY address DESC
a. 加limit。
b. 只读取需要的字段。
c. EXISTS改为IN。

6. 是否了解XML?讲一下格式规范。

7. 是否使用PHP解析过XML文件?使用的什么方法?该方法使用的是什么API?(没有听懂后面半句是什么意思)

8. 写一个正则表达式

9. 一个3k+1数组,k为非负整数,请问从1乘到7000,结果的末尾会包含多少个0?

Share on Facebook

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>