


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.
Q1: What is the difference between == and ===?
Topic: PHP
Difficulty: ⭐
- If they are two different types, operator
==
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
#Q2: How to pass variables by reference?
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
#Q3: What does $GLOBAL mean?
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
Q4: What is the use of ini_set()?
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
##Q5: When should I use require and include?
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
Q6: What is stdClass in PHP?
Theme: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-phpQ7: die() and exit() in PHP ) What is the difference between functions? Topic:
PHPinstead 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:
Q8:它们之间的主要区别是什么
话题: 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
Q9: isset() 和 array_key_exists()之间有什么区别?
话题: PHP
困难: ⭐⭐
array_key_exists
它会告诉你数组中是否存在键,并在$a
不存在时报错。- 如果 key或变量存在且不是
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
Q10: var_dump() 和 print_r() 有什么不同?
话题: 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
Q11: 解释不同的 PHP 错误是什么
话题: PHP
困难: ⭐⭐
notice
不是一个严重的错误,它说明执行过程中出现了一些错误,一些次要的错误,比如一个未定义的变量。- 当出现更严重的错误,如include()命令引入不存在的文件时,会给出警告
warning
。 这个错误和上面的错误发生,脚本都将继续。 fatal error
致命错误将终止代码。未能满足require()将生成这种类型的错误。
? 源自: https://pangara.com/blog/php-interview-questions
Q12: 如何在 PHP 中启用错误报告?
话题: 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
Q13: 使用默认参数声明某些函数
话题: PHP
困难: ⭐⭐
思考:
function showMessage($hello = false){ echo ($hello) ? 'hello' : 'bye'; }
? 源自: https://www.codementor.io/blog/php-interview-questions-sample-answers-du1080ext
Q14: PHP 是否支持多重继承?
话题: PHP
困难: ⭐⭐
PHP 只支持单一继承;这意味着使用关键字’extended’只能从一个类扩展一个类。
? 源自: https://www.guru99.com/php-interview-questions-answers.html
Q15: 在 PHP 中,对象是按值传递还是按引用传递?
话题: PHP
困难: ⭐⭐
在 PHP 中,通过值传递的对象。
? 源自: https://www.guru99.com/php-interview-questions-answers.html
Q16:$a != $b 和 $a !== $b ,之间有什么区别?
话题: PHP
困难: ⭐⭐
!=
表示 不等于 (如果$a不等于$b,则为 True), !==
表示 不全等 (如果$a与$b不相同,则为 True).
? 源自: https://www.guru99.com/php-interview-questions-answers.html
Q17: 在 PHP 中,什么是 PDO?
话题: PHP
困难: ⭐⭐
PDO 代表 PHP 数据对象。
它是一组 PHP 扩展,提供核心 PDO 类和数据库、特定驱动程序。它提供了供应商中立、轻量级的数据访问抽象层。因此,无论我们使用哪种数据库,发出查询和获取数据的功能都是相同的。它侧重于数据访问抽象,而不是数据库抽象。
? 源自: https://github.com/Bootsity/cracking-php-interviews-book
Q18: 说明我们如何在PHP中处理异常?
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
Q19: 区分echo和print()
Topic: PHP
Difficulty: ⭐⭐
echo
和 print
基本上是一样的. 他们都是用来打印输出数据的。
区别在于:
- echo没有返回值,而print的返回值为1,因此print可以在表达式中使用。
- echo可以接受多个参数一起输出(但是这种多个的输出方式很少见),而print一次只可以输出一个参数。
- echo 的输出比 print 效率要高一些 .
? Source: https://github.com/Bootsity/cracking-php-interviews-book
Q20: require_once 和 require 在什么场景下使用?
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
Q21: 判断PHP数组是否是关联数组
Topic: PHP
Difficulty: ⭐⭐⭐
思考:
function has_string_keys(array $array) { return count(array_filter(array_keys($array), 'is_string')) > 0; }
如果$array
至少有一个字符串类型的 key ,它将被视为关联数组。
? Source: stackoverflow.com
Q22: 如何将变量和数据从PHP传至Javascript
Topic: PHP
Difficulty: ⭐⭐⭐
这里有几种实现方法:
- 使用 Ajax 从服务端获取你需要的数据。
思考 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>
- 可以在网页任何地方输出数据, 然后使用 JavaScript 从 DOM 中获取信息.
<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>
- 直接在 JavaScript 代码中 echo 数据。
<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
Q23: 有一个方法可以复制一个 PHP 数组至另一个数组吗?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
