Home >Backend Development >PHP Tutorial >20+ PHP interview questions worth knowing (with answer analysis)
This article will share with you more than 20 PHP interview questions, check for any omissions and fill in the gaps, and help you consolidate the foundation. See how many of them you can answer correctly? I hope to be helpful.
Topic: PHP
Difficulty: ⭐
==
Force conversion between two different types===
The operator performs 'type safety comparison'This means that it will return TRUE only if both operands have the same type and the same value.
1 === 1: true 1 == 1: true 1 === "1": false // 1 是一个整数, "1" 是一个字符串 1 == "1": true // "1" 强制转换为整数,即1 "foo" === "foo": true // 这两个操作数都是字符串,并且具有相同的值
? From: https://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals -comp
Topic: PHP
Difficulty: ⭐
To be able to pass variables by reference, we Use & in front of it, like this:
$var1 = &$var2
? From: https://www.guru99.com/php-interview-questions-answers .html
Topic: PHP
Difficulty: ⭐
$GLOBALS
is an associative array containing pairs References to all variables currently defined in the global scope of the script.
? From: https://www.guru99.com/php-interview-questions-answers.html
Topic: PHP
Difficulty: ⭐
PHP allows users to use ini_set() to modify php.ini as mentioned in some settings. This function requires two string parameters. The first is the name of the setting to be modified, and the second is the new value to be assigned to it.
The given line of code will enable the script's display_error setting if it is disabled.
ini_set('display_errors', '1');
We need to put the above statement at the top of the script so that the setting remains enabled until at last. Additionally, values set via ini_set() only apply to the current script. After this, PHP will start using the original value from php.ini.
? From: https://github.com/Bootsity/cracking-php-interviews-book
Topic:##require()PHPDifficulty: ⭐⭐
Function and include()
The function is the same, but the way it handles errors is different. If an error occurs, the include()
function generates a warning, but the script continues execution. require()
The function will generate a fatal error and the script will stop. My advice is to only use
99.9% of the time. Using
or include
instead means that your code is not reusable elsewhere, i.e. the script you include actually executes the code rather than providing the class Or some class function library. ?
https://stackoverflow.com/questions/2418473/difference-between-require-include-require-once-and-include-once
PHPJust cast other types Generic "empty" class used when being an object.Difficulty: ⭐⭐
##stdClass
stdClass is not the base class for objects in PHP. This can be easily proven:
class Foo{} $foo = new Foo(); echo ($foo instanceof stdClass)?'Y':'N'; // 输出'N'
is useful for anonymous objects, dynamic properties, etc. .
A simple usage scenario to consider StdClass
is to replace an associative array. See the example below which shows howjson_decode() allows getting a StdClass instance or Associative array.
Also but not shown in this example SoapClient::__soapCall
returns a
StdClass instance.
//带有StdClass的示例 $json = '{ "foo": "bar", "number": 42 }'; $stdInstance = json_decode($json); echo $stdInstance - > foo.PHP_EOL; //"bar" echo $stdInstance - > number.PHP_EOL; //42 //Example with associative array $array = json_decode($json, true); echo $array['foo'].PHP_EOL; //"bar" echo $array['number'].PHP_EOL; //42
?
From: https://stackoverflow.com/questions/931407/what-is-stdclass-in-php
instead ofDifficulty: ⭐⭐
die()
No difference, they are the same . The only benefit of choosing
exit() is probably that you save the time of typing an extra letter.
?
from:
话题: PHP
困难: ⭐⭐
const
和define
的根本区别在于,const
在编译时定义常量,而define
在运行时定义常量。
const FOO = 'BAR'; define('FOO', 'BAR'); // but if (...) { const FOO = 'BAR'; // 无效 } if (...) { define('FOO', 'BAR'); // 有效 }
同样在PHP 5.3之前,const
命令不能在全局范围内使用。你只能在类中使用它。当你想要设置与该类相关的某种常量选项或设置时,应使用此选项。或者你可能想要创建某种枚举。一个好的const
用法的例子是摆脱了魔术数字。
Define
可以用于相同的目的,但只能在全局范围内使用。它应该仅用于影响整个应用程序的全局设置。
除非你需要任何类型的条件或表达式定义,否则请使用consts
而不是define()
——这仅仅是为了可读性!
? 源自: https://stackoverflow.com/questions/2447791/define-vs-const
话题: PHP
困难: ⭐⭐
array_key_exists
它会告诉你数组中是否存在键,并在$a
不存在时报错。null
,isset
才会返回true
。当$a
不存在时,isset
不会报错。考虑:
$a = array('key1' => 'Foo Bar', 'key2' => null); isset($a['key1']); // true array_key_exists('key1', $a); // true isset($a['key2']); // false array_key_exists('key2', $a); // true
? 源自: https://stackoverflow.com/questions/3210935/whats-the-difference-between-isset-and-array-key-exists
话题: PHP
困难: ⭐⭐
var_dump
函数用于显示变量/表达式的结构化信息,包括变量类型和变量值。数组递归浏览,缩进值以显示结构。它还显示哪些数组值和对象属性是引用。
print_r()
函数以我们可读的方式显示有关变量的信息。数组值将以键和元素的格式显示。类似的符号用于对象。
考虑:
$obj = (object) array('qualitypoint', 'technologies', 'India');
var_dump($obj)
将在屏幕的输出下方显示:
object(stdClass)#1 (3) { [0]=> string(12) "qualitypoint" [1]=> string(12) "technologies" [2]=> string(5) "India" }
print_r($obj)
将在屏幕的输出下方显示。
stdClass Object ( [0] => qualitypoint [1] => technologies [2] => India )
? 源自: https://stackoverflow.com/questions/3406171/php-var-dump-vs-print-r
话题: PHP
困难: ⭐⭐
notice
不是一个严重的错误,它说明执行过程中出现了一些错误,一些次要的错误,比如一个未定义的变量。warning
。 这个错误和上面的错误发生,脚本都将继续。fatal error
致命错误将终止代码。未能满足require()将生成这种类型的错误。? 源自: https://pangara.com/blog/php-interview-questions
话题: PHP
困难: ⭐⭐
检查 php.ini 中的“display_errors
”是否等于“on”,或者在脚本中声明“ini_set('display_error',1)
”。
然后,在你的代码中包含“ERROR_REPORTING(E_ALL)
”,以便在脚本执行期间显示所有类型的错误消息。
? 源自: https://www.codementor.io/blog/php-interview-questions-sample-answers-du1080ext
话题: PHP
困难: ⭐⭐
思考:
function showMessage($hello = false){ echo ($hello) ? 'hello' : 'bye'; }
? 源自: https://www.codementor.io/blog/php-interview-questions-sample-answers-du1080ext
话题: PHP
困难: ⭐⭐
PHP 只支持单一继承;这意味着使用关键字’extended’只能从一个类扩展一个类。
? 源自: https://www.guru99.com/php-interview-questions-answers.html
话题: PHP
困难: ⭐⭐
在 PHP 中,通过值传递的对象。
? 源自: https://www.guru99.com/php-interview-questions-answers.html
话题: PHP
困难: ⭐⭐
!=
表示 不等于 (如果$a不等于$b,则为 True), !==
表示 不全等 (如果$a与$b不相同,则为 True).
? 源自: https://www.guru99.com/php-interview-questions-answers.html
话题: PHP
困难: ⭐⭐
PDO 代表 PHP 数据对象。
它是一组 PHP 扩展,提供核心 PDO 类和数据库、特定驱动程序。它提供了供应商中立、轻量级的数据访问抽象层。因此,无论我们使用哪种数据库,发出查询和获取数据的功能都是相同的。它侧重于数据访问抽象,而不是数据库抽象。
? 源自: https://github.com/Bootsity/cracking-php-interviews-book
Topic: PHP
Difficulty: ⭐⭐
当程序执行出现异常报错时,后面的代码将不会再执行,这时PHP将会尝试匹配第一个catch块进行异常的处理,如果没有捕捉到异常程序将会报致命错误并显示”Uncaught Exception”。
可以在PHP中抛出和捕获异常。
为了处理异常,代码可以被包围在”try”块中.
每个 try 必须至少有一个对应的 catch
块 。多个不同的catch块可用于捕获不同类的异常。
在catch块中也可以抛出异常(或重新抛出之前的异常)。
思考:
try { print "this is our try block n"; throw new Exception(); } catch (Exception $e) { print "something went wrong, caught yah! n"; } finally { print "this part is always executed n"; }
? Source: https://github.com/Bootsity/cracking-php-interviews-book
Topic: PHP
Difficulty: ⭐⭐
echo
和 print
基本上是一样的. 他们都是用来打印输出数据的。
区别在于:
? Source: https://github.com/Bootsity/cracking-php-interviews-book
Topic: PHP
Difficulty: ⭐⭐⭐
require_once()
作用与 require()
的作用是一样的,都是引用或包含外部的一个php文件,require_once()
引入文件时会检查文件是否已包含,如果已包含,不再包含(require)它。
我建议在99.9%的时候要使用 require_once
使用require
或 include
意味着您的代码不可在其他地方重用,即您要拉入的脚本实际上是在执行代码,而不是提供类或某些函数库。
? Source: https://stackoverflow.com/questions/2418473/difference-between-require-include-require-once-and-include-once
Topic: PHP
Difficulty: ⭐⭐⭐
思考:
function has_string_keys(array $array) { return count(array_filter(array_keys($array), 'is_string')) > 0; }
如果$array
至少有一个字符串类型的 key ,它将被视为关联数组。
? Source: stackoverflow.com
Topic: PHP
Difficulty: ⭐⭐⭐
这里有几种实现方法:
思考 get-data.php:
echo json_encode(42);
思考 index.html:
<script> function reqListener () { console.log(this.responseText); } var oReq = new XMLHttpRequest(); // new 一个请求对象 oReq.onload = function() { // 在这里你可以操作响应数据 // 真实的数据来自 this.responseText alert(this.responseText); // 将提示: 42 }; oReq.open("get", "get-data.php", true); // ^ 不要阻塞的其余部分执行。 // 不要等到请求结束再继续。 oReq.send(); </script>
<div id="dom-target" style="display: none;"> <?php $output = "42"; // 此外, 做一些操作,获得 output. echo htmlspecialchars($output); /* 你必须避免特殊字符,不然结果将是无效HTML。 */ ?> </div> <script> var div = document.getElementById("dom-target"); var myData = div.textContent; </script>
<script> var data = <?php echo json_encode("42", JSON_HEX_TAG); ?>; // Don't forget the extra semicolon! </script>
? Source: https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript
Topic: PHP
Difficulty: ⭐⭐⭐
PHP 数组通过复制进行赋值,而对象通过引用进行赋值。所有默认情况下,PHP 将复制这个数组。这里有一个 PHP 参考,一目了然:
$a = array(1,2); $b = $a; // $b 是一个不同的数组 $c = &$a; // $c 是 $a 的引用
? Source: https://stackoverflow.com/questions/1532618/is-there-a-function-to-make-a-copy-of-a-php-array-to-another
英文原文地址:https://dev.to/fullstackcafe/45-important-php-interview-questions-that-may-land-you-a-job-1794
推荐学习:《PHP视频教程》
The above is the detailed content of 20+ PHP interview questions worth knowing (with answer analysis). For more information, please follow other related articles on the PHP Chinese website!