Home  >  Article  >  Backend Development  >  100 common PHP interview questions (with analysis) to enhance your knowledge reserve!

100 common PHP interview questions (with analysis) to enhance your knowledge reserve!

青灯夜游
青灯夜游forward
2022-05-11 10:45:1430591browse

This article compiles and shares 100 common PHP interview questions (with answers to share) to help you sort out basic knowledge and enhance your PHP knowledge reserve. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

100 common PHP interview questions (with analysis) to enhance your knowledge reserve!

Related recommendations: 2022 PHP interview questions summary (collection)

100 Common PHP Interview Questions

1) What is PHP?

PHP is a script-based web language that allows developers to dynamically create web pages.

2) What is the full name of PHP?

Hypertext Preprocessor.

3) What programming languages ​​is PHP similar to?

PHP syntax is similar to Perl and C.

5) Which version of PHP is actually used?

It is recommended to use version 7.

6) How to execute PHP script from command line?

In the command line interface (CLI), specify the file name of the script to be executed as follows:

php script.php

7) How to run interactive from the command line interface PHP Shell?

Use the PHP CLI program with the -a option as follows:

php -a

8) The two most common ways to properly start and end PHP code blocks What is it?

The two most common ways to start and end a PHP script are:

 <?php [   ---  PHP code---- ] ?> and <? [---  PHP code  ---] ?>

9) How to display the output directly to the browser?

To display the output directly to the browser, we must use the special tag .

11) Does PHP support multiple inheritance?

PHP only supports single inheritance. PHP classes use the keyword extends to inherit another class

12) What do classes and methods modified with final mean?

final was introduced in the PHP5 version. The classes it modifies are not allowed to be inherited, and the methods it modifies are not allowed to be overridden.

13) How to compare two objects in PHP?

In PHP, we can use the operator == to compare whether two objects are instances of the same class and have the same attributes and attribute values.
You can also use the operator === to compare whether two objects refer to the same instance of the same class.

14) How do PHP and HTML interact?

HTML can be generated through PHP scripts, and information can also be passed from HTML to PHP.

15) What type of operations are required when passing values ​​through a form or URL?

Passing values ​​through a form or URL, you need to encode and decode them using htmlspecialchars() and urlencode().

16) How do PHP and Javascript interact?

PHP and Javascript cannot interact directly because PHP is a server-side language and Javascript is a browser language. However, we can exchange variables because PHP can generate Javascript code that will be executed by the browser and specific variables can be passed back to PHP via the URL.

17) What extensions need to be added to PHP to process images?

The GD library is required to perform image processing functions.

18) What is the function of function imagetypes()?

imagetypes() Gives the image formats and types supported by the current version of GD-PHP.

19) What is the function to get the image attributes (size, width, and height)?

Get the image size size: getimagesize(); Get the image width width: imagesx(); Get the image height height: imagesy().

20) What is the difference between include() and require() in case of execution failure?

include() will generate a warning and will not affect the execution of subsequent programs. require() will generate a fatal error and subsequent program execution will stop.

21) The main difference between require() and require_once() is?

require() and require_once() perform the same task, except for the second The function checks whether the PHP script is included before executing it.

(Same as include_once() and include())

22) How to use PHP script to display text?

You can use the following two methods :

<!--?php echo "Method 1"; print "Method 2"; ?-->

23) How to display variable information and make it human-readable using PHP?

To be able to display human-readable results, we use print_r().

24) How to set unlimited execution time for PHP script?

Add set_time_limit(0) at the beginning of the script to set unlimited execution time to avoid PHP error "Exceeded" Maximum execution time". It can also be specified in the php.ini file.

25) PHP error 'Parse error in PHP - unexpected T_variable at line x' means?

This is a PHP syntax error, indicating that the error at line x will Stop parsing and executing the program.

26) How to export data to Excel file?

最常见和常用的方法是将数据转换为Excel支持的格式。例如,可以编写 .csv 文件,例如选择逗号作为字段之间的分隔符,然后使用 Excel 打开文件。

27) file_get_contents() 函数的作用是?

file_get_contents() 可读取文件并将其存储到字符串变量中。

28) 如何使用 PHP 脚本 连接 MySQL 数据库?

为了连接到 MySQL 数据库,必须使用 mysql_connect() 函数:

<!--?php $database = mysqli_connect("HOST", "USER_NAME", "PASSWORD"); mysqli_select_db($database,"DATABASE_NAME"); ?-->

29) mysql_pconnect() 函数的用途为?

mysql_pconnect() 函数确保与数据库的持久连接, 这意味着 PHP 脚本结束时连接不会关闭。

PHP7.0 及以上版本已不支持该函数。

30) 如何在 PHP 中处理 MySQL 的结果集?

可以使用 mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_object or mysqli_fetch_row 函数处理。

31) 如何知晓结果集返回的行数?

mysqli_num_rows() 函数返回了结果集的行数。

32) 哪个函数为我们提供了查询所影响的条数?

mysqli_affected_rows() 返回受SQL查询影响的条目数。

33) mysqli_fetch_object() 和 mysqli_fetch_array() 函数的区别在于?

mysqli_fetch_object() 函数收集第一个单个匹配记录,而  mysqli_fetch_array() 从表中收集所有匹配记录。

34) 如何使用 GET 方法访问通过 URL 发送的数据?

要访问通过 GET 方法发送的数据,我们使用 $ _GET 数组,如下所示:

www.url.com?var=value
$variable = $_GET["var"]; 现在将包含 'value'

35) 如何使用 POST 方法访问通过 URL 发送的数据?

要访问以这种方式发送的数据,请使用 $ _POST 数组。

想象一下,当用户单击「提交到帖子」表单时,表单上有一个名为「var」的表单字段,然后您可以像这样访问值:

$_POST["var"];

36) 如何检查给定变量的值为数字?

可以使用专用函数 is_numeric() 来检查它是否为数字。

37) 如何检查给定变量的值为字母和数字字符?

可以使用专用函数 ctype_alnum 来检查它是否为字母数字字符。

38) 如何检查给定变量是否为空?

如果我们要检查变量是否具有值,可以使用 empty() 函数。

39) unlink() 函数的作用是?

unlink() 函数专用于文件系统处理。它用于删除文件。

40) unset() 函数的作用是 ?

unset() 函数专用于变量管理。它将使变量变为未定义。

41) 在将数据存储到数据库之前如何转义数据?

addslashes 函数使我们能够在将数据存储到数据库之前对其进行转义。

42) 如何从字符串中删除转义字符?

使用 stripslash 函数,我们可以删除字符串中的转义字符。

43) 我们如何自动转义传入的数据?

我们必须在PHP的配置文件中启用魔术引号项。

44) get_magic_quotes_gpc() 函数的作用是?

函数 get_magic_quotes_gpc() 告诉我们魔术引号是否已打开。

45) 是否可以从数据中删除 HTML 标签?

strip_tags() 函数使我们能够从HTML标签中清除字符串。

46) 函数中的静态变量有什么用?

静态变量仅在函数中首次定义,并且其值可以在函数调用期间进行如下修改:

<!--?php function testFunction() { static $testVariable = 1; echo $testVariable; $testVariable++; } testFunction();        //1 testFunction();        //2 testFunction();        //3 ?-->

47) 如何定义PHP脚本函数中可访问的变量??

使用 global 关键字。

48) 如何从函数中返回值?

使用指令 ‘return $value;’ 。

49) 用于哈希密码最便捷的哈希方法是什么?

最好使用本身支持几种哈希算法的 crypt() 或支持比 crypt() 更多变体的函数hash(),而不是使用常见的哈希算法,如 MD5、SHA1 或 sha256,因为它们被认为是存在安全问题的。因此,使用这些算法的哈希密码可能会产生漏洞。

50) 哪种加密扩展可以生成和验证数字签名?

PHP-OpenSSL扩展提供了几种加密操作,包括数字签名的生成和验证。

51) 如何在 PHP 脚本中定义常量?

define() 指令允许我们按如下方式定义常量:

define ("ACONSTANT", 123);

52) 如何通过引用传递变量?

为了能够通过引用传递变量,我们在变量前面使用了 & 符号,如下所示$var1=&$var2

53) 整数12和字符串“13”的比较在PHP中是否有效?

“13” 和 12 可以在 PHP 中进行比较,因为它将所有内容都强制转换为整数类型。

54) 如何在PHP中强制转换类型?

输出类型的名称必须在要强制转换的变量前的括号中指定,如下所示:

  • (int), (integer) - 强制转换为整型

  • (bool), (boolean) - 强制转换为布尔值

  • (float), (double), (real) - 强制转换为浮点型

  • (string) - 强制转换为字符串

  • (array) - 强制转换为数组

  • (object) - 强制转换为对象

55) 条件语句何时以 endif 结尾?

当最初的if后面跟着:然后是没有大括号的代码块时。

56) PHP中如何使用三元条件运算符?

它由三个表达式组成:一个条件和两个操作数,它们描述在指定条件为true或false时应执行的指令,如下所示:

Expression_1?Expression_2 : Expression_3;

57) 函数 func_num_args() 的作用是什么?

函数 func_num_args() 用于提供传递给函数的参数数量

58) 如果变量$ var1设置为10,而$ var2设置为字符var1,那么$$ var2的值是多少?

$$var2 包含值10。

59) 通过::访问类意味着什么?

::用于访问不需要对象初始化的静态方法。

60) 在 PHP 中,对象是按值传递还是按引用传递?

对象按值传递。

** 61)是否在类构造函数中隐式调用了Parent构造函数?**

不,必须显式调用父构造函数,如下所示:

parent::constructor($value)

** 62)__sleep__wakeup有什么区别?**

__sleep返回所有需要保存的变量的数组,而__wakeup检索它们。

** 63)更快些吗?**

1-结合以下两个变量:

$ variable1 ='你好';$ variable2 ='世界';$ variable3 = $ variable1.$ variable2;

要么

2- $variable3 = "$variable1$variable2";

$variable3将包含“你好 世界”。第一个代码比第二个代码快,特别是对于大型数据集。

** 64)会话的定义是什么?**

会话是一个逻辑对象,使我们能够跨多个PHP页面保留临时数据。

** 65)如何在PHP中启动会话?**

使用session_start()函数可以激活会话。

** 66)如何传播会话ID?**

您可以通过CookieURL参数传播会话ID。

** 67)永久性Cookie的含义是什么?**

永久性cookie永久存储在浏览器计算机上的cookie文件中。默认情况下,cookies是临时的,如果我们关闭浏览器,cookies将被删除。

** 68)会议何时结束?**

会话在PHP脚本完成执行时自动结束,但可以使用session_write_close()手动结束。

** 69)session_unregister()session_unset()有什么区别?**

session_unregister()函数从当前会话中注销全局变量,而session_unset()函数则释放所有会话变量。

** 70)$GLOBALS是什么意思?**

$GLOBALS是一个关联数组,包括对当前在脚本的全局范围内定义的所有变量的引用。
71) $ _SERVER 是什么意思?

$_SERVER  是一个包含 Web 服务器创建信息的数组,包括了路径,头部和脚本位置等。

72)$ _FILES 是什么意思?

$_FILES 是一个包含通过 HTTP POST 方式上传到当前脚本的项目的数组。

73)$ _FILES ['userfile'] ['name' ]$ _FILES ['userfile'] ['tmp_name'] 有什么区别

$ _FILES ['userfile'] ['name']  表示客户端文件的原始名称,

$_FILES['userfile']['tmp_name'] 表示服务器上存储的文件的临时文件名。

*74) 上传文件出问题时,如何获取错误信息 *

$_FILES['userfile']['error']  包括了与上传文件有关的错误代码。

75)如何更改要上传的文件大小的最大值?

可通过更改  php.ini 中的 upload_max_filesize 来更改要上传的文件的最大大小。

76)$ _ENV 是什么意思?

通过环境方式传递给当前脚本的变量的数组。

77)$ _COOKIE 是什么意思?

通过 HTTP Cookies 方式传递给当前脚本的变量的数组。

78)变量的作用域是什么意思?

变量的作用域定义了变量的环境上下文。在大多数情况下,PHP 变量只有一个变量域。这一作用域也涵盖了 includerequire 的文件。

79)’BITWISE AND’ 运算符和 ‘LOGICAL AND’ 运算符之间有什么区别?

$a and $b -  只有在 $a$b 都为 true 的情况下才为 true
$a & $b -  把 $a$b 中都为 1 的位设为 1

80)两个主要的字符串运算符是什么?

. 运算符返回左右两边字符串的拼接结果。 .= 运算符将右边的结果附加到左边的参数上。

81) 数组运算符  ‘===’  是什么意思?

$a === $b 如果 $a 和 $b 具有相同顺序和相同类型的键/值对,则为 TRUE 。

82)  $a != $b 和 $a !== $b有什么区别?

!=  表示不等于 ( 如果 $a 不等于 $b 则为TRUE) ; !== 表示 不全等 ( 如果$a 不完全等于 $b则为TRUE).

83) 如何确定 PHP 变量是否是某个类的实例化对象?

我们用  instanceof 能够验证 PHP 变量是否是某个类的实例话对象。

84) goto 语句有什么用?

goto语句可以放置在PHP程序中以启用跳转。 目标由后跟冒号的标签指向,指令被指定为goto语句,后跟所需的目标标签。

85) Exception::getMessage 和 Exception:: getLine有什么区别?

Exception::getMessage 让我们得到异常消息,  Exception::getLine 让我们得到发生异常的行。

86) 表达式 Exception::__toString 是什么意思?

Exception::__toString 给出异常的字符串表示形式。

87) 如何解析配置文件?

函数 parse_ini_file() 使我们能够加载在文件名中指定的 ini 文件,并以关联数组的形式返回其中的设置。

88) 我们如何确定变量是否已经设置?

布尔函数 isset 确定变量是否已经设置并且该变量不是NULL。

89) 函数 strstr() 和 stristr() 有什么区别?

字符串函数 strstr(全部字符串, 要查找的字符串) 返回从首次出现到全部字符串结束的部分字段串。 这个函数是区分大小写的。 stristr() 除了不区分大小写之外,与 strstr() 完全相同。

90) for 和 foreach 有什么区别?

for 表示如下:

for (expr1; expr2; expr3)

expr1 在开头执行一次。 在每次迭代中,expr2 都会被评估。 如果评估结果是 TRUE, 循环继续, 并执行 for 中的语句。 如果评估结果是 FALSE, 循环结束。expr3 在每次迭代结束时进行测试。

但是,foreach提供了一种遍历数组的简便方法,并且仅与数组和对象一起使用。

91) 是否可以提交带有专用按钮的表单?

可以使用 document.form.submit() 函数提交表单。 例如:

92)  ereg_replace() 和 eregi_replace()有什么区别?

函数eregi_replace() 与 ereg_replace() 相同,只是在匹配字母字符时忽略大小写区别。

93) 是否可以保护查询字符串中的特殊字符?

是的, 我们使用 urlencode() 函数 来保护特殊字符。

94) PHP 中可能出现的三类错误是什么?

三类基本错误类别是: 通知 (非关键错误)、警告 (严重错误) 和 致命错误 (严重错误)。

95) 字符串 .34 和 .34 有什么区别

.34 是八进制 34 , .34 是十六进制 34.

96) 我们如何通过页面之间的导航传递变量?

可以使用会话,cookie 或隐藏的表单字段在 PHP 页面之间传递变量。

97) 是否可以延长 PHP 脚本的执行时间

使用 set_time_limit(int seconds) 可以延长 PHP 脚本的执行时间。 默认限制为30秒。

98) 是否可以销毁 Cookie ?

是的,可以通过设置cookie的过期时间来实现。

99) PHP中的默认会话时间是什么?

php中的默认会话时间是直到浏览器关闭为止。

100) 是否可以在 PHP中 使用 COM 组件?

是的, 可以在框架中提供的 PHP 脚本中集成(分布式)组件对象模型组件((D)COM)。

101) 解释是否可以在多个 PHP 项目之间共享单个 memcache 实例?

Yes, it is possible to share a Memcache instance between multiple projects. Memcache is a memory storage space that you can run on one or more servers. You can also configure the client to talk to a specific set of instances. Therefore, you can run two different Memcache processes on the same host, but they are completely independent. Unless you have partitioned your data, it is necessary to know which instance to get the data from or into.

102) Explain how you update Memcached when you make changes to PHP?

When PHP changes, you can update Memcached by

  • Actively clear the cache: Clear the cache when an insert or update is made
  • Reset Cache: Similar to the first method, but instead of just deleting the key and waiting for the next data refresh cache request, you reset the value after an insert or update.

Original address: https://www.guru99.com/php-interview-questions-answers.html

Translation address: https:// learnku.com/php/t/38272

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of 100 common PHP interview questions (with analysis) to enhance your knowledge reserve!. 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