Home>Article>Backend Development> 20+ PHP interview questions worth knowing (with answer analysis)

20+ PHP interview questions worth knowing (with answer analysis)

青灯夜游
青灯夜游 forward
2022-06-02 11:15:28 5539browse

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.

20+ PHP interview questions worth knowing (with answer analysis)

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 byreference, 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: ⭐

$GLOBALSis 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:

PHPDifficulty: ⭐⭐

##require()

Function andinclude()The function is the same, but the way it handles errors is different. If an error occurs, theinclude()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

require_once

99.9% of the time.Using

require

orincludeinstead 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.?

Source:

https://stackoverflow.com/questions/2418473/difference-between-require-include-require-once-and-include-once

Q6: What is stdClass in PHP?

Theme:
PHP

Difficulty: ⭐⭐

##stdClass
Just cast other types Generic "empty" class used when being an object.

stdClassis 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 how

json_decode()allows getting a StdClass instance or Associative array.Also but not shown in this exampleSoapClient::__soapCallreturns a
StdClassinstance.

//带有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

Q7: die() and exit() in PHP ) What is the difference between functions?Topic:

PHP

Difficulty: ⭐⭐
No difference, they are the same . The only benefit of choosing

die()
instead of

exit()is probably that you save the time of typing an extra letter.?from:

https://stackoverflow.com/questions/1795025/what-are-the-differences-in-die-and-exit-in-php

Q8:它们之间的主要区别是什么

话题:PHP
困难: ⭐⭐

constdefine的根本区别在于,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或变量存在且不是nullisset才会返回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: ⭐⭐

echoprint基本上是一样的. 他们都是用来打印输出数据的。

区别在于:

  • 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

使用requireinclude意味着您的代码不可在其他地方重用,即您要拉入的脚本实际上是在执行代码,而不是提供类或某些函数库。

?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:

  • 可以在网页任何地方输出数据, 然后使用 JavaScript 从 DOM 中获取信息.
 
  • 直接在 JavaScript 代码中 echo 数据。

?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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete