Preparing for PHP interviews or strengthening your server-side development skills? This PHP MCQs collection covers frequently tested PHP concepts, practical coding scenarios, modern PHP features, and secure development practices. Use these multiple-choice questions to check your knowledge and identify areas to revise.
Note: Score 60% or higher to unlock an exclusive offer of up to 50% off on all self-paced PHP courses.
Let's begin!
1. What does PHP stand for?
2. Which file extension is commonly used for PHP files?
3. Which opening tag is commonly used to start PHP code?
4. Which statement outputs text in PHP?
5. Which symbol begins a PHP variable name?
6. Which character usually ends a PHP statement?
7. Which function returns the type and value details of a variable?
8. Which operator concatenates strings in PHP?
9. Which keyword declares a constant at runtime?
10. Which function checks whether a variable is set and is not null?
11. Which function can display a human-readable representation of a variable?
12. Which PHP construct can output a string and return a value?
13. Which is a valid PHP variable name?
14. Which value represents a Boolean true value?
15. What does the === operator check?
16. Which operator checks inequality without type coercion?
17. What is the result type of 7 / 2 in PHP?
18. Which function counts elements in an array?
19. Which function converts a value to an integer?
20. Which operator is commonly used for null coalescing?
21. What does the spaceship operator <=> return?
22. Which value indicates that a variable has no value?
23. Which operator is used for exponentiation?
24. What does PHP use to interpolate variables inside a double-quoted string?
25. Which function returns the length of a string in bytes?
26. Which function finds the position of a substring?
27. Which function replaces occurrences of text in a string?
28. Which function splits a string using a delimiter?
29. Which function joins array elements into a string?
30. Which function adds one or more elements to the end of an array?
31. Which function removes and returns the last array element?
32. Which syntax creates an associative array entry?
33. Which function checks whether a key exists in an array?
34. Which function sorts an array by values while maintaining key associations?
35. Which function filters array elements using a callback?
36. What does array_map() primarily do?
37. Which construct selects among multiple conditions?
38. Which loop is designed to iterate over arrays?
39. Which keyword exits the current loop immediately?
40. Which keyword skips to the next loop iteration?
41. When is a do-while loop condition checked?
42. Which operator is the ternary conditional operator?
43. What does match expression use to compare values?
44. Which keyword starts an alternative conditional branch?
45. What does return do inside a function?
46. Which statement can terminate the entire PHP script?
47. What is the purpose of a default branch in switch?
48. Which loop is guaranteed to execute its body at least once?
49. Which keyword declares a user-defined function?
50. How can a function receive a parameter by reference?
51. Which keyword imports a global variable into a function scope?
52. What does a static local variable retain between function calls?
53. Which syntax provides a default parameter value?
54. What is an anonymous function commonly called in PHP?
55. Which keyword allows an anonymous function to capture an outer variable?
56. What does a variadic parameter allow?
57. Which syntax declares an arrow function?
58. What does a return type declaration specify?
59. What is recursion?
60. Which built-in function checks whether a function exists?
61. Which keyword declares a class?
62. Which operator accesses an instance property or method?
63. Which operator accesses a static class member?
64. Which method is called when an object is created?
65. Which visibility allows access only within the declaring class?
66. Which visibility allows access in the class and its subclasses?
67. Which keyword indicates class inheritance?
68. Which keyword declares that a class follows an interface?
69. What is a trait used for in PHP?
70. Which magic method is invoked when inaccessible properties are read?
71. What does the final keyword prevent on a class?
72. Which keyword refers to the current object instance?
73. Which superglobal usually contains data submitted with the POST method?
74. Which superglobal contains query-string parameters?
75. Which function starts or resumes a session?
76. Which superglobal stores session variables?
77. Which function sends a cookie to the browser?
78. Why should header() generally be called before output is sent?
79. Which function safely encodes special HTML characters for output?
80. Which superglobal contains uploaded-file metadata?
81. What should be checked before trusting submitted form data?
82. What is CSRF protection intended to prevent?
83. Which function can regenerate a session ID?
84. Where should sensitive session data generally be stored?
85. What does PDO stand for in PHP?
86. What is a key benefit of prepared statements?
87. Which PDO method prepares an SQL statement?
88. Which PDO method executes a prepared statement?
89. Which approach is recommended for connecting to a database securely?
90. What does a database transaction help ensure?
91. Which PDO method can fetch the next row as an array?
92. Which SQL statement retrieves rows from a table?
93. Which SQL command adds a new row?
94. Why should user input not be concatenated directly into SQL?
95. Which PDO attribute enables exception-based error handling?
96. What should an application do with database errors shown to end users?
97. What is the safest general defense against SQL injection in PHP?
98. What helps prevent cross-site scripting (XSS) when displaying user content?
99. Which password API should be used to hash user passwords?
100. Which function verifies a password against a password hash?
101. Why is MD5 unsuitable for storing passwords?
102. What does HTTPS help protect in transit?
103. Which setting should generally be disabled in production?
104. What is the purpose of try/catch?
105. Which construct runs code whether an exception occurs or not?
106. What is the principle of least privilege for a PHP application?
107. Which function compares two strings in a timing-attack-aware way?
108. Why should secrets not be committed to a public repository?
109. What is Composer primarily used for?
110. Which file commonly defines Composer package requirements?
111. What is the purpose of composer.lock?
112. Which command typically installs dependencies from composer.lock?
113. What is PSR-4 commonly associated with?
114. Which keyword declares a namespace?
115. Which built-in function returns the PHP version?
116. Which tool is commonly used for PHP unit testing?
117. What is a PHP interface primarily used to define?
118. What is the purpose of strict_types declaration?
119. Which feature allows a function to pause and yield values over time?
120. Which PHP function converts a JSON string into a PHP value?
121. Which function reads a file into a string?
122. Which function writes data to a file?
123. Which function checks whether a file exists?
124. Which function decodes JSON into an associative array when configured to do so?
125. Which function converts a PHP value to a JSON string?
126. Which superglobal provides information about the request, headers, and server environment?
127. Which function sends an HTTP status code?
128. What is the purpose of cURL in PHP?
129. Which response format is widely used by web APIs and supported by PHP JSON functions?
130. Why should external API responses be validated before use?
131. Which function escapes shell arguments for safer command-line use?
132. Which approach is best for handling uploaded files?