21个PHP面试必备问题 *

最优秀的PHP开发人员和工程师可以回答的基本问题. 在我们社区的推动下,我们鼓励专家提交问题并提供反馈.

现在就聘请一名顶级PHP开发人员
Toptal logo是顶级自由软件开发人员的专属网络吗, designers, finance experts, product managers, 和世界上的项目经理. 顶级公司雇佣Toptal自由职业者来完成他们最重要的项目.

Interview Questions

1.

它们之间有什么区别 echo and print in PHP?

View answer

echo and print are largely the same in PHP. 两者都用于向屏幕输出数据.

唯一的区别如下:

  1. echo 不返回值,反之 print 返回1的值(这启用 print to be used in expressions).
  2. echo 可以接受多个参数(尽管这种用法很少见),而print只能接受一个参数.
2.

这段代码将输出什么,为什么?

$x = true and false;
var_dump($x);
View answer

令许多人惊讶的是,上面的代码将输出 bool(true) seeming to imply that the and 操作符的行为是 or.

The issue here is that the = 操作符优先于 and 运算符按运算顺序排列,所以语句 $x = true and false 最终在功能上等同于:

$x = true;       // sets $x equal to true
true and false;  // results in false, but has no affect on anything

This is, incidentally, 这是一个很好的例子,说明为什么使用括号来清楚地说明你的意图通常是一个很好的做法, in any language. 例如,如果上面的语句 $x = true and false were replaced with $x = (true and false), then $x would be set to false as expected.

3.

下面代码的输出是什么?为什么?

$x = 5;
echo $x;
echo "
"; echo $x+++$x++; echo "
"; echo $x; echo "
"; echo $x---$x--; echo "
"; echo $x;
View answer

输出如下所示:

5
11
7
1
5

以下是解释原因的两个关键事实:

  1. The term $x++ 表示使用的当前值 $x and then increment it. Similarly, the term $x-- 表示使用的当前值 $x and then decrement it.
  2. The increment operator (++)的优先级高于求和运算符(+) in order of operations.

有了这些要点,我们就可以理解了 $x+++$x++ 的第一次引用是 $x 当它的值还是5 (i.e., before 的第二次引用 $x 当它的值是6 (i.e., before it is again 递增),所以操作是 5 + 6 which yields 11. 执行该操作后, $x 7是因为加了两次吗.

同样,我们可以理解这一点 $x---$x-- 的第一次引用是 $x 当它的值还是7 (i.e., before 的第二次引用 $x 当它的值是6 (i.e., before it is again 自减),所以操作是 7 - 6 which yields 1. 执行该操作后, $x 是回到它的原始值5,因为它已经增加了两次,然后减少了两次.

申请加入Toptal的发展网络

并享受可靠、稳定、远程 自由PHP开发人员职位

Apply as a Freelancer
4.

What will be the values of $a and $b 在执行下面的代码之后? Explain your answer.

$a = '1';
$b = &$a;
$b = "2$b";
View answer

Both $a and $b will be equal to the string "21" 在执行上述代码之后.

Here’s why:

The statement $b = &$a; sets $b equal to a reference to $a (as opposed to setting $b to the then-current value of $a). Thereafter, as long as $b remains a reference to $a, anything done to $a will affect $b and vice versa.

所以当我们随后执行语句时 $b = "2$b", $b is set equal to the string "2" 的当前值 $b (which is the same as $a) which is 1, so this results in $b 被设置为等于字符串 "21" (i.e., the concatenation of "2" and "1"). And, since $b is a reference to $a,这对的值有相同的影响 $a, so both end up equal to "21".

5.

下面每个语句的输出是什么?为什么?

var_dump(0123 == 123);
var_dump('0123' == 123);
var_dump('0123' === 123);
View answer

var_dump(0123 == 123) will output bool(false) because the leading 0 in 0123 告诉PHP解释器将该值视为八进制(而不是十进制)值, 八进制123等于十进制83, so the values are not equal.

var_dump('0123' == 123) will output bool(true) since the string 0123 与整数值比较时会自动强制为整数吗. 有趣的是,当执行此转换时,前导 0 被忽略,并将该值视为十进制(而不是八进制)值, 所以这两个值是123(十进制),因此是相等的.

var_dump('0123' === 123) outputs bool(false) 因为它执行更严格的比较,不会自动将字符串类型强制转换为整数.

6.

下面的代码有什么问题? What will it output? How can it be fixed?

$referenceTable = array();
$referenceTable['val1'] = array(1, 2);
$referenceTable['val2'] = 3;
$referenceTable['val3'] = array(4,5);

$testArray = array();

$testArray = array_merge($testArray, $referenceTable['val1']);
var_dump($testArray);
$testArray = array_merge($testArray, $referenceTable['val2']);
var_dump($testArray);
$testArray = array_merge($testArray, $referenceTable['val3']);
var_dump($testArray);
View answer

输出如下所示:

array(2) { [0]=> int(1) [1]=> int(2) }
NULL
NULL

您可能还会看到生成的两个警告,类似于以下内容:

警告:array_merge():参数#2不是数组
警告:array_merge():参数#1不是数组

The issue here is that, if either 的第一个或第二个参数 array_merge() 不是数组,返回值会是 NULL. 例如,尽管人们可能合理地期望这样的调用 array_merge (someValidArray美元,零) would simply return $someValidArray, it instead returns NULL! (更糟糕的是,这一点在 PHP documentation.)

As a result, the call to $testArray = array_merge($testArray, $referenceTable['val2']) evaluates to $testArray = array_merge($testArray, 3) and, since 3 is not of type array, this call to array_merge() returns NULL,这反过来又会导致 $testArray equal to NULL. 然后,当我们接到下一个电话 array_merge(), $testArray is now NULL so array_merge() again returns NULL. (这也解释了为什么第一个警告抱怨参数#2,第二个警告抱怨参数#1.)

解决这个问题的方法很简单. 如果简单地将第二个实参类型转换为an array,我们就会得到想要的结果. The corrected array_merge() 因此,呼吁如下:

$testArray = array_merge($testArray, (array)$referenceTable['val1']);
var_dump($testArray);
$testArray = array_merge($testArray, (array)$referenceTable['val2']);
var_dump($testArray);
$testArray = array_merge($testArray, (array)$referenceTable['val3']);
var_dump($testArray);

这将产生以下输出(没有警告):

array(2) { [0]=> int(1) [1]=> int(2) } 
array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } 
array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }
7.

What will $x 等于语句后面的 $x = 3 + "15%" + "$25"?

View answer

The correct answer is 18.

Here’s why:

PHP supports automatic type conversion 基于使用变量或值的上下文.

如果对包含字符串的表达式执行算术运算, 为了计算表达式,该字符串将被解释为适当的数字类型. So, 如果字符串以一个或多个数字字符开头, 字符串的剩余部分(如果有的话)将被忽略,数值将被解释为适当的数字类型. 另一方面,如果字符串以非数字字符开头,那么它的值将为零.

有了这样的理解,我们就能看到 "15%" 计算结果为数值15和 "$25" 计算结果为数值零,这解释了为什么语句的结果 $x = 3 + "15%" + "$25" is 18 (i.e., 3 + 15 + 0).

请注意,从PHP 7开始.2、此代码产生错误.

8.

执行下面的代码后,的值将是什么 $text and what will strlen($text) return? Explain your answer.

$text = 'John ';
$text[10] = 'Doe';
View answer

执行上述代码后, $text will be the string “John D” (i.e.“John”,后面跟着六个空格,后面跟着“D”)和 strlen($text) will return 11.

这里发生了两件事.

First of all, since $text 是一个字符串,设置一个元素的 $text 简单地将单个字符设置为指定的值. The statement $text[10] = 'Doe' 因此将单个字符设置为 'D' (i.e.字符串中的第一个字符 "Doe",因为字符串的一个元素只能是单个字符).

Secondly, $text[10] = 'Doe' 表示将字符串的第11个字符(请记住,索引是从零开始的)设置为 'D'. 但是,在该语句之前,字符串的长度 $text ("John ") was only five. 而其他语言中的编译器或解释器在尝试设置5个字符串的第11个字符时可能会出现问题(类似于超出索引范围的错误), 相反,PHP非常“包容”,允许这样做,并将所有中间字符设置为空白.

9.

PHP_INT_MAX 是一个PHP常量,对应于支持的最大整数值(该值基于正在运行的PHP版本及其运行的平台)。.

Assume that var_dump(PHP_INT_MAX) will yield int(9223372036854775807).

在这种情况下,结果会是什么 var_dump(PHP_INT_MAX + 1)? 还有,结果会是什么 var_dump((int)(PHP_INT_MAX + 1))?

NOTE: 在回答问题时提供确切的值并不重要, 而是解释会发生什么以及为什么会发生.

View answer

The result of var_dump(PHP_INT_MAX + 1) 将显示为双精度类型(在这个特定的示例中,它将显示 double(9.2233720368548E+18)). 这里的关键是让候选人知道PHP通过将大整数转换为双精度(可以存储更大的值)来处理大整数。.

有趣的是,结果 var_dump((int)(PHP_INT_MAX + 1)) 将显示为一个负数(在这个特定的示例中,它将显示 int(-9223372036854775808)). Again, 这里的关键是让候选人知道该值将显示为负数, 不知道精确的值.

10.

下面的代码回显什么?

$a = "PHP";
$a = $a + 1;
echo $a;
View answer

The number 1.

Note, in PHP 7.这将抛出一个警告,表示该行遇到了非数字值,并且不会进行到 int, so it echoes PHP instead.

11.

如何将字符串数组排序到不区分大小写的自然顺序, 同时保持它们原来的索引关联?

例如,下面的数组:

array(
	'0' => 'z1',
	'1' => 'Z10',
	'2' => 'z12',
	'3' => 'Z2',
	'4' => 'z3',
)

整理后,应变成:

array(
	'0' => 'z1',
	'3' => 'Z2',
	'4' => 'z3',
	'1' => 'Z10',
	'2' => 'z12',
)
View answer

解决这个问题的诀窍是在标准中使用三个特殊的标志 asort() library function:

带点(arr美元,SORT_STRING | SORT_FLAG_CASE | SORT_NATURAL)

The function asort() 是标准函数的变体吗 sort() 这保留了索引关联. The three flags used above SORT_STRING, SORT_FLAG_CASE and SORT_NATURAL 强制排序函数将项视为字符串, 以不区分大小写的方式排序,并分别保持自然顺序.

Note: Using the natcasesort() 函数不是正确答案, 因为它不会维护数组元素的原始索引关联.

12.

What are Traits?

View answer

trait是一种机制,它在PHP等不支持多重继承的语言中提供了多重继承的一些重用优势. trait使开发人员能够重用来自不同类层次结构的方法组合.

13.

What is PEAR in php?

View answer

PEAR (PHP Extension and Application R(repository)是可重用PHP组件的框架和存储库. PEAR是一个包含各种php代码片段和库的代码存储库.

PEAR还提供了一个命令行接口,可用于自动安装包.

14.

What is use of the header() function in PHP?

View answer
  1. header() 用于从一个页面重定向到另一个页面: header("Location: index.php");
  2. header() 用于发送HTTP状态码: header("HTTP/1.0 this Not Found");
  3. header() 用于发送原始HTTP报头: 标题(“application / json - type:”);
15.

考虑下面的代码:

$x = NULL;

if ('0xFF' == 255) {
    $x = (int)'0xFF';
}

What will be the value of $x after this code executes? Explain your answer.

View answer

也许令人惊讶的是,答案既不是NULL也不是255. Rather, the answer is that $x will equal 0 (zero).

Why?

首先,让我们考虑是否 '0xFF' == 255 求值为真还是假. 当将十六进制字符串松散地与整数进行比较时,将其转换为整数. Internally, PHP uses is_numeric_string 检测字符串是否包含十六进制值并将其转换为整数(因为另一个操作数是整数). 所以在这种情况下,' 0xFF '被转换成它的等效整数,也就是255. 由于255 = 255,因此该条件的计算结果为true. (注意,这只适用于十六进制字符串,不适用于八进制或二进制字符串.)

但如果是这样的话,语句不应该 $x = (int)'0xFF'; execute and result in $x being set equal to 255?

语句确实执行了,但是结果是 $x being set equal to 0, not 255 (i.e., it is not 设置为相当于' 0xFF '的整数). 原因是将字符串显式类型强制转换为整数使用 convert_to_long (它的工作原理与 is_numeric_string 函数,用于对条件表达式求值,如上所述). convert_to_long 从左到右处理字符串,每次处理一个字符,并在到达的第一个非数字字符处停止. In the case of ‘0xFF’, 第一个非数字字符是' x ', 所以字符串中唯一被处理的部分就是初始的' 0 '. 返回的值 (int)'0xFF' 是0,所以当代码完成时, $x will be equal to 0.

16.

如何在不使用任何条件或循环的情况下判断一个数字是偶数还是奇数?

View answer
$arr=array("0"=>"Even","1"=>"Odd");

$check=13;

echo "Your number is: ".$arr[$check%2];
17.

两者的区别是什么 include_once() and require_once()在需要连接到数据库的情况下,您会使用哪一个,以及为什么?

View answer

include_once() or include 允许包含文件, 在文件丢失或名称错误的情况下, 我们收到一个错误消息,执行仍然会继续.

On the other hand, require_once() or require 是否适合在需要包含一次文件的情况下,如果它丢失或有错误的名称,那么我们会收到一个致命的错误,程序的执行停止.

require_once or require 在涉及数据库连接文件的情况下,是否有一种合适的方法可以帮助减轻同一文件的多个实例被多次包含的可能性.

18.

会话和cookie之间的区别是什么?

View answer

会话将值存储在服务器上,cookie将值存储在用户的浏览器中.

19.
$str = '喝酒给慢跑喝喝传球制图给跑步吃';  // Example input 

用红色突出显示所有中文字符并返回字符串.

View answer
$str = '喝酒给慢跑喝喝传球制图给跑步吃';
$string = explosion (' ', $str);
$chi = array_filter(explosion ('_', preg_replace(array('/[^\p{Han]))?/u', '/(\s)+/'), array('_', '$1'), $str));
$value = array ();
foreach ($string as $s) {
           if (in_array($s, $chi)) {
               $value[] = ''.$s.'';
           } else {
               $value[] = $s;
           }
}

返回(内爆(' ',$value));
20.

编写一个示例代码,展示PHP中嵌套的三元条件运算符.

View answer
$number_class = $number == 0 ? 'blue' : ($number > 0 ? 'green' : 'red');

这里我们将不同的字符串赋值给 number_class 基于数值的变量($number).

21.

考虑下面的代码:

$str1 = 'yabadabadoo';
$str2 = 'yaba';
if (strpos($str1,$str2)) {
    echo "\"" . $str1 . "\" contains \"" . $str2 . "\"";
} else {
    echo "\"" . $str1 . "\" does not contain \"" . $str2 . "\"";
}

The output will be:

"yabadabadoo"不包含"yaba"

Why? 如何修复此代码以正确工作?

View answer

The problem here is that strpos() 的起始位置索引 $str2 in $str1 (如果找到),否则返回 false. So in this example, strpos() returns 0 (which is then coerced to false when referenced in the if statement). 这就是代码不能正常工作的原因.

的返回值进行显式比较是正确的解决方案 strpos() to false as follows:

$str1 = 'yabadabadoo';
$str2 = 'yaba';
if (strpos($str1,$str2) !== false) {
    echo "\"" . $str1 . "\" contains \"" . $str2 . "\"";
} else {
    echo "\"" . $str1 . "\" does not contain \"" . $str2 . "\"";
}

Note that we used the !== operator, not just the != operator. If we use !=,我们会回到这个问题 0 is coerced to false 在布尔表达式中引用时,so 0 != false will evaluate to false.

面试不仅仅是棘手的技术问题, 所以这些只是作为一个指南. 并不是每一个值得雇佣的“A”候选人都能回答所有的问题, 回答所有问题也不能保证成为A级考生. At the end of the day, 招聘仍然是一门艺术,一门科学,需要大量的工作.

Why Toptal

厌倦了面试候选人? 不知道该问什么才能让你得到一份好工作?

让Toptal为你找到最合适的人.

现在就聘请一名顶级PHP开发人员

我们的PHP开发者专属网络

希望找到一份作为PHP开发人员的工作?

让Toptal为你找到合适的工作.

Apply as a PHP Developer

工作机会从我们的网络

提出面试问题

提交的问题和答案将被审查和编辑, 并可能会或可能不会选择张贴, 由Toptal全权决定, LLC.

* All fields are required

Looking for PHP Developers?

Looking for PHP Developers? 查看Toptal的PHP开发人员.

Jay Johnston

Freelance PHP Developer
United StatesToptal的自由PHP开发人员 Since November 26, 2013

Coding HTML, CSS, 他从1997年参军时就开始学习JavaScript了, Jay喜欢通过电子商务解决方案为客户带来价值, legacy integrations, 以及优化的PHP和javascript驱动的应用程序. 他首选的DevOps环境是AWS, 他在关系数据库服务(RDS)方面有很强的技能(但不限于):, Redshift, Dynamo DB, 数据迁移服务(DMS), Lambda(无服务器和微服务), Cloudwatch, Cloudtrail, and Event Bridge.

Show More

Igor Santos

Freelance PHP Developer
BrazilToptal的自由PHP开发人员 Since February 15, 2016

Igor是一名专注于现代PHP和JS的web开发人员,始终致力于新技术. 当他戴上后帽的时候, 他专注于高性能和DRY代码, 在API服务器或消费者上工作. 当他戴上帽子的时候, 他致力于为用户提供最好的用户体验.

Show More

David Marín

Freelance PHP Developer
SpainToptal的自由PHP开发人员 Since July 2, 2015

David是一名开源和开放数据爱好者,拥有超过23年的专业开发经验. 他掌握了各种技能, 包括网页编程(PHP和JavaScript), C, c++ (Linux和Windows下), and systems management. Of these skills, David擅长web编程,并拥有基于Symfony php的后端全栈经验, jQuery front ends, 以及基于WordPress/ woocommerce的网站.

Show More

Toptal Connects the Top 3% 世界各地的自由职业人才.

Join the Toptal community.

Learn more