


100 common PHP interview questions (with analysis) to enhance your knowledge reserve!
May 11, 2022 am 10:45 AMThis 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.
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 =and? >.
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?
最常見和常用的方法是將數(shù)據(jù)轉(zhuǎn)換為Excel支持的格式。例如,可以編寫 .csv 文件,例如選擇逗號作為字段之間的分隔符,然后使用 Excel 打開文件。
27) file_get_contents() 函數(shù)的作用是?
file_get_contents() 可讀取文件并將其存儲到字符串變量中。
28) 如何使用 PHP 腳本 連接 MySQL 數(shù)據(jù)庫?
為了連接到 MySQL 數(shù)據(jù)庫,必須使用 mysql_connect() 函數(shù):
<!--?php $database = mysqli_connect("HOST", "USER_NAME", "PASSWORD"); mysqli_select_db($database,"DATABASE_NAME"); ?-->
29) mysql_pconnect() 函數(shù)的用途為?
mysql_pconnect() 函數(shù)確保與數(shù)據(jù)庫的持久連接, 這意味著 PHP 腳本結(jié)束時連接不會關(guān)閉。
PHP7.0 及以上版本已不支持該函數(shù)。
30) 如何在 PHP 中處理 MySQL 的結(jié)果集?
可以使用 mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_object or mysqli_fetch_row 函數(shù)處理。
31) 如何知曉結(jié)果集返回的行數(shù)?
mysqli_num_rows() 函數(shù)返回了結(jié)果集的行數(shù)。
32) 哪個函數(shù)為我們提供了查詢所影響的條數(shù)?
mysqli_affected_rows() 返回受SQL查詢影響的條目數(shù)。
33) mysqli_fetch_object() 和 mysqli_fetch_array() 函數(shù)的區(qū)別在于?
mysqli_fetch_object() 函數(shù)收集第一個單個匹配記錄,而 ?mysqli_fetch_array() 從表中收集所有匹配記錄。
34) 如何使用 GET 方法訪問通過 URL 發(fā)送的數(shù)據(jù)?
要訪問通過 GET 方法發(fā)送的數(shù)據(jù),我們使用 $ _GET 數(shù)組,如下所示:
www.url.com?var=value $variable?=?$_GET["var"];?現(xiàn)在將包含?'value'
35) 如何使用 POST 方法訪問通過 URL 發(fā)送的數(shù)據(jù)?
要訪問以這種方式發(fā)送的數(shù)據(jù),請使用 $ _POST 數(shù)組。
想象一下,當用戶單擊「提交到帖子」表單時,表單上有一個名為「var」的表單字段,然后您可以像這樣訪問值:
$_POST["var"];
36) 如何檢查給定變量的值為數(shù)字?
可以使用專用函數(shù) is_numeric() 來檢查它是否為數(shù)字。
37) 如何檢查給定變量的值為字母和數(shù)字字符?
可以使用專用函數(shù) ctype_alnum 來檢查它是否為字母數(shù)字字符。
38) 如何檢查給定變量是否為空?
如果我們要檢查變量是否具有值,可以使用 empty() 函數(shù)。
39) unlink() 函數(shù)的作用是?
unlink() 函數(shù)專用于文件系統(tǒng)處理。它用于刪除文件。
40) unset() 函數(shù)的作用是 ?
unset() 函數(shù)專用于變量管理。它將使變量變?yōu)槲炊x。
41) 在將數(shù)據(jù)存儲到數(shù)據(jù)庫之前如何轉(zhuǎn)義數(shù)據(jù)?
addslashes 函數(shù)使我們能夠在將數(shù)據(jù)存儲到數(shù)據(jù)庫之前對其進行轉(zhuǎn)義。
42) 如何從字符串中刪除轉(zhuǎn)義字符?
使用 stripslash 函數(shù),我們可以刪除字符串中的轉(zhuǎn)義字符。
43) 我們?nèi)绾巫詣愚D(zhuǎn)義傳入的數(shù)據(jù)?
我們必須在PHP的配置文件中啟用魔術(shù)引號項。
44) get_magic_quotes_gpc() 函數(shù)的作用是?
函數(shù) get_magic_quotes_gpc() 告訴我們魔術(shù)引號是否已打開。
45) 是否可以從數(shù)據(jù)中刪除 HTML 標簽?
strip_tags() 函數(shù)使我們能夠從HTML標簽中清除字符串。
46) 函數(shù)中的靜態(tài)變量有什么用?
靜態(tài)變量僅在函數(shù)中首次定義,并且其值可以在函數(shù)調(diào)用期間進行如下修改:
<!--?php function testFunction() { static $testVariable = 1; echo $testVariable; $testVariable++; } testFunction(); //1 testFunction(); //2 testFunction(); //3 ?-->
47) 如何定義PHP腳本函數(shù)中可訪問的變量??
使用 global 關(guān)鍵字。
48) 如何從函數(shù)中返回值?
使用指令 ‘return $value;’ 。
49) 用于哈希密碼最便捷的哈希方法是什么?
最好使用本身支持幾種哈希算法的 crypt() 或支持比 crypt() 更多變體的函數(shù)hash(),而不是使用常見的哈希算法,如 MD5、SHA1 或 sha256,因為它們被認為是存在安全問題的。因此,使用這些算法的哈希密碼可能會產(chǎn)生漏洞。
50) 哪種加密擴展可以生成和驗證數(shù)字簽名?
PHP-OpenSSL擴展提供了幾種加密操作,包括數(shù)字簽名的生成和驗證。
51) 如何在 PHP 腳本中定義常量?
define() 指令允許我們按如下方式定義常量:
define?("ACONSTANT",?123);
52) 如何通過引用傳遞變量?
為了能夠通過引用傳遞變量,我們在變量前面使用了 & 符號,如下所示$var1=&$var2
53) 整數(shù)12和字符串“13”的比較在PHP中是否有效?
“13” 和 12 可以在 PHP 中進行比較,因為它將所有內(nèi)容都強制轉(zhuǎn)換為整數(shù)類型。
54) 如何在PHP中強制轉(zhuǎn)換類型?
輸出類型的名稱必須在要強制轉(zhuǎn)換的變量前的括號中指定,如下所示:
(int), (integer) - 強制轉(zhuǎn)換為整型
(bool), (boolean) - 強制轉(zhuǎn)換為布爾值
(float), (double), (real) - 強制轉(zhuǎn)換為浮點型
(string) - 強制轉(zhuǎn)換為字符串
(array) - 強制轉(zhuǎn)換為數(shù)組
(object) - 強制轉(zhuǎn)換為對象
55) 條件語句何時以 endif 結(jié)尾?
當最初的if后面跟著:然后是沒有大括號的代碼塊時。
56) PHP中如何使用三元條件運算符?
它由三個表達式組成:一個條件和兩個操作數(shù),它們描述在指定條件為true或false時應(yīng)執(zhí)行的指令,如下所示:
Expression_1?Expression_2?:?Expression_3;
57) 函數(shù) func_num_args() 的作用是什么?
函數(shù) func_num_args() 用于提供傳遞給函數(shù)的參數(shù)數(shù)量
58) 如果變量$ var1設(shè)置為10,而$ var2設(shè)置為字符var1,那么$$ var2的值是多少?
$$var2 包含值10。
59) 通過::訪問類意味著什么?
::用于訪問不需要對象初始化的靜態(tài)方法。
60) 在 PHP 中,對象是按值傳遞還是按引用傳遞?
對象按值傳遞。
** 61)是否在類構(gòu)造函數(shù)中隱式調(diào)用了Parent
構(gòu)造函數(shù)?**
不,必須顯式調(diào)用父構(gòu)造函數(shù),如下所示:
parent::constructor($value)
** 62)__sleep
和__wakeup
有什么區(qū)別?**
__sleep
返回所有需要保存的變量的數(shù)組,而__wakeup
檢索它們。
** 63)更快些嗎?**
1-結(jié)合以下兩個變量:
$?variable1?='你好';$?variable2?='世界';$?variable3?=?$?variable1.$?variable2;
要么
2-?$variable3?=?"$variable1$variable2";
$variable3
將包含“你好 世界”。第一個代碼比第二個代碼快,特別是對于大型數(shù)據(jù)集。
** 64)會話的定義是什么?**
會話是一個邏輯對象,使我們能夠跨多個PHP頁面保留臨時數(shù)據(jù)。
** 65)如何在PHP中啟動會話?**
使用session_start()
函數(shù)可以激活會話。
** 66)如何傳播會話ID?**
您可以通過Cookie
或URL
參數(shù)傳播會話ID。
** 67)永久性Cookie
的含義是什么?**
永久性cookie
永久存儲在瀏覽器計算機上的cookie
文件中。默認情況下,cookies
是臨時的,如果我們關(guān)閉瀏覽器,cookies
將被刪除。
** 68)會議何時結(jié)束?**
會話在PHP腳本完成執(zhí)行時自動結(jié)束,但可以使用session_write_close()
手動結(jié)束。
** 69)session_unregister()
和session_unset()
有什么區(qū)別?**
session_unregister()
函數(shù)從當前會話中注銷全局變量,而session_unset()
函數(shù)則釋放所有會話變量。
** 70)$GLOBALS
是什么意思?**
$GLOBALS
是一個關(guān)聯(lián)數(shù)組,包括對當前在腳本的全局范圍內(nèi)定義的所有變量的引用。
71) $ _SERVER
是什么意思?
$_SERVER
?是一個包含 Web 服務(wù)器創(chuàng)建信息的數(shù)組,包括了路徑,頭部和腳本位置等。
72)$ _FILES
是什么意思?
$_FILES
是一個包含通過 HTTP POST 方式上傳到當前腳本的項目的數(shù)組。
73)$ _FILES ['userfile'] ['name' ]
和 $ _FILES ['userfile'] ['tmp_name']
有什么區(qū)別
$ _FILES ['userfile'] ['name']
?表示客戶端文件的原始名稱,
$_FILES['userfile']['tmp_name']
表示服務(wù)器上存儲的文件的臨時文件名。
*74) 上傳文件出問題時,如何獲取錯誤信息 *
$_FILES['userfile']['error']
?包括了與上傳文件有關(guān)的錯誤代碼。
75)如何更改要上傳的文件大小的最大值?
可通過更改 ?php.ini
中的 upload_max_filesize
來更改要上傳的文件的最大大小。
76)$ _ENV
是什么意思?
通過環(huán)境方式傳遞給當前腳本的變量的數(shù)組。
77)$ _COOKIE
是什么意思?
通過 HTTP Cookies 方式傳遞給當前腳本的變量的數(shù)組。
78)變量的作用域是什么意思?
變量的作用域定義了變量的環(huán)境上下文。在大多數(shù)情況下,PHP 變量只有一個變量域。這一作用域也涵蓋了 include
與 require
的文件。
79)’BITWISE AND’ 運算符和 ‘LOGICAL AND’ 運算符之間有什么區(qū)別?
$a and $b
- ?只有在 $a
和 $b
都為 true
的情況下才為 true
$a & $b
- ?把 $a
和 $b
中都為 1 的位設(shè)為 1
80)兩個主要的字符串運算符是什么?
.
運算符返回左右兩邊字符串的拼接結(jié)果。 .=
運算符將右邊的結(jié)果附加到左邊的參數(shù)上。
81) 數(shù)組運算符 ?‘===’ ?是什么意思?
$a === $b 如果 $a 和 $b 具有相同順序和相同類型的鍵/值對,則為 TRUE 。
82) ?$a != $b 和 $a !== $b有什么區(qū)別?
!= ?表示不等于 ( 如果 $a 不等于 $b 則為TRUE) ; !== 表示 不全等 ( 如果$a 不完全等于 $b則為TRUE).
83) 如何確定 PHP 變量是否是某個類的實例化對象?
我們用 ?instanceof 能夠驗證 PHP 變量是否是某個類的實例話對象。
84) goto 語句有什么用?
goto語句可以放置在PHP程序中以啟用跳轉(zhuǎn)。 目標由后跟冒號的標簽指向,指令被指定為goto語句,后跟所需的目標標簽。
85) Exception::getMessage 和 Exception:: getLine有什么區(qū)別?
Exception::getMessage 讓我們得到異常消息, ?Exception::getLine 讓我們得到發(fā)生異常的行。
86) 表達式 Exception::__toString 是什么意思?
Exception::__toString 給出異常的字符串表示形式。
87) 如何解析配置文件?
函數(shù) parse_ini_file() 使我們能夠加載在文件名中指定的 ini 文件,并以關(guān)聯(lián)數(shù)組的形式返回其中的設(shè)置。
88) 我們?nèi)绾未_定變量是否已經(jīng)設(shè)置?
布爾函數(shù) isset 確定變量是否已經(jīng)設(shè)置并且該變量不是NULL。
89) 函數(shù) strstr() 和 stristr() 有什么區(qū)別?
字符串函數(shù) strstr(全部字符串, 要查找的字符串) 返回從首次出現(xiàn)到全部字符串結(jié)束的部分字段串。 這個函數(shù)是區(qū)分大小寫的。 stristr() 除了不區(qū)分大小寫之外,與 strstr() 完全相同。
90) for 和 foreach 有什么區(qū)別?
for 表示如下:
for?(expr1;?expr2;?expr3)
expr1 在開頭執(zhí)行一次。 在每次迭代中,expr2 都會被評估。 如果評估結(jié)果是 TRUE, 循環(huán)繼續(xù), 并執(zhí)行 for 中的語句。 如果評估結(jié)果是 FALSE, 循環(huán)結(jié)束。expr3 在每次迭代結(jié)束時進行測試。
但是,foreach提供了一種遍歷數(shù)組的簡便方法,并且僅與數(shù)組和對象一起使用。
91) 是否可以提交帶有專用按鈕的表單?
可以使用 document.form.submit() 函數(shù)提交表單。 例如:
92) ?ereg_replace() 和 eregi_replace()有什么區(qū)別?
函數(shù)eregi_replace() 與 ereg_replace() 相同,只是在匹配字母字符時忽略大小寫區(qū)別。
93) 是否可以保護查詢字符串中的特殊字符?
是的, 我們使用 urlencode() 函數(shù) 來保護特殊字符。
94) PHP 中可能出現(xiàn)的三類錯誤是什么?
三類基本錯誤類別是: 通知 (非關(guān)鍵錯誤)、警告 (嚴重錯誤) 和 致命錯誤 (嚴重錯誤)。
95) 字符串 .34 和 .34 有什么區(qū)別
.34 是八進制 34 , .34 是十六進制 34.
96) 我們?nèi)绾瓮ㄟ^頁面之間的導(dǎo)航傳遞變量?
可以使用會話,cookie 或隱藏的表單字段在 PHP 頁面之間傳遞變量。
97) 是否可以延長 PHP 腳本的執(zhí)行時間
使用 set_time_limit(int seconds) 可以延長 PHP 腳本的執(zhí)行時間。 默認限制為30秒。
98) 是否可以銷毀 Cookie ?
是的,可以通過設(shè)置cookie的過期時間來實現(xiàn)。
99) PHP中的默認會話時間是什么?
php中的默認會話時間是直到瀏覽器關(guān)閉為止。
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!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

Select the appropriate AI voice recognition service and integrate PHPSDK; 2. Use PHP to call ffmpeg to convert recordings into API-required formats (such as wav); 3. Upload files to cloud storage and call API asynchronous recognition; 4. Analyze JSON results and organize text using NLP technology; 5. Generate Word or Markdown documents to complete the automation of meeting records. The entire process needs to ensure data encryption, access control and compliance to ensure privacy and security.
