PHP Technology & Digital Applications 4 — Questions and Answers
Question 1: Which PHP magic method is called when an inaccessible property is read?
- __set()
- __get() (Correct answer)
- __isset()
- __call()
Correct answer: __get()
__get() is triggered when reading data from inaccessible or non-existent properties of an object.
Question 2: What HTTP status code should a PHP script return when a resource is not found?
- 200
- 301
- 404 (Correct answer)
- 500
Correct answer: 404
HTTP 404 (Not Found) indicates the server could not find the requested resource.
Question 3: Which PHP function permanently redirects a browser to a new URL?
- redirect()
- header('Location: url') (Correct answer)
- go_to()
- http_redirect()
Correct answer: header('Location: url')
header('Location: url') sends a redirect header; adding HTTP/1.1 301 makes it permanent.
Question 4: In PHP, what is the difference between `include` and `require`?
- require allows multiple inclusions; include does not
- include causes a fatal error on failure; require does not
- require causes a fatal error on failure; include only emits a warning (Correct answer)
- They are identical in behavior
Correct answer: require causes a fatal error on failure; include only emits a warning
require causes a fatal E_COMPILE_ERROR if the file is not found, while include only produces an E_WARNING and continues execution.
Question 5: Which PHP function hashes a password using a strong one-way algorithm?
- md5()
- sha1()
- password_hash() (Correct answer)
- crypt()
Correct answer: password_hash()
password_hash() uses bcrypt by default and is the recommended way to securely hash passwords in PHP.
Question 6: What does PHP's `array_map()` function do?
- Filters array elements by a condition
- Sorts an array by value
- Applies a callback to each element and returns the results (Correct answer)
- Merges two arrays together
Correct answer: Applies a callback to each element and returns the results
array_map() applies a given callback function to each element of one or more arrays and returns the modified array.
Question 7: Which PHP directive in php.ini controls the maximum size of an uploaded file?
- post_max_size
- max_upload_size
- upload_max_filesize (Correct answer)
- file_limit
Correct answer: upload_max_filesize
upload_max_filesize sets the maximum allowed size for a single uploaded file in PHP.
Which PHP magic method is called when an inaccessible property is read?