PHP Research & Evidence-Based Practice 5 — Questions and Answers
Question 1: Which PHP OPcache function forces the current cached version of a file to be invalidated, useful when testing if new code changes are picked up?
- opcache_reset()
- opcache_invalidate() (Correct answer)
- opcache_flush()
- opcache_clear_cache()
Correct answer: opcache_invalidate()
opcache_invalidate() removes a specific file from the OPcache, forcing it to be recompiled on the next request without clearing the entire cache.
Question 2: In PHP unit testing, what is a 'test double' and why is it used in evidence-based practice?
- Running the same test twice for validation
- A substitute object that replaces a real dependency during testing (Correct answer)
- A PHP script that duplicates production data
- A backup test in case the primary test fails
Correct answer: A substitute object that replaces a real dependency during testing
A test double (mock, stub, spy, or fake) replaces real dependencies so tests isolate and verify a single unit of behavior without side effects.
Question 3: Which PHP function is used to read the contents of an external URL for research data gathering, requiring allow_url_fopen to be enabled?
- curl_exec()
- file_get_contents() (Correct answer)
- http_get()
- fetch_url()
Correct answer: file_get_contents()
file_get_contents() can retrieve remote URL content when allow_url_fopen is enabled in php.ini, though cURL is preferred for complex requests.
Question 4: When analyzing PHP application logs for patterns and error rates, which log format is most machine-readable and supports structured querying?
- Plain text Apache-style log
- JSON-formatted log entries (Correct answer)
- HTML error output
- Syslog plain string format
Correct answer: JSON-formatted log entries
JSON-formatted logs allow log aggregators like Elasticsearch, Datadog, or Graylog to parse, index, and query individual fields programmatically.
Question 5: Which PHP class from the standard library provides an iterator for traversing directory contents, useful in scripts that research file structures?
- DirectoryReader
- FilesystemIterator (Correct answer)
- DirScanner
- FileTree
Correct answer: FilesystemIterator
FilesystemIterator extends DirectoryIterator and provides a clean OOP interface for iterating directory contents with configurable flags.
Question 6: In PHP security research, which function should be used to safely compare two hashes to prevent timing attacks?
- strcmp()
- hash_equals() (Correct answer)
- md5_compare()
- === operator directly
Correct answer: hash_equals()
hash_equals() performs a constant-time string comparison that prevents timing-based side-channel attacks when comparing cryptographic hashes.
Question 7: Which PHP array function is most appropriate for collecting research metrics by transforming each element of a dataset and returning a new array?
- array_walk()
- array_map() (Correct answer)
- array_reduce()
- array_filter()
Correct answer: array_map()
array_map() applies a callback to each element and returns a new array with the transformed values, leaving the original unchanged.
Which PHP OPcache function forces the current cached version of a file to be invalidated, useful when testing if new code changes are picked up?