PHP Research & Evidence-Based Practice 4 — Questions and Answers
Question 1: Which PHP configuration directive, when enabled, reveals all errors including notices and warnings, which is essential during research and development phases?
- display_errors = On with error_reporting = E_ALL (Correct answer)
- log_errors = On
- error_reporting = E_ERROR only
- display_startup_errors = On alone
Correct answer: display_errors = On with error_reporting = E_ALL
Setting display_errors = On combined with error_reporting = E_ALL ensures every type of error, including notices and deprecations, is displayed during development.
Question 2: What PHP feature allows a developer to document code using a standardized format that tools like phpDocumentor can parse to generate API documentation?
- Inline comments with //
- DocBlocks with /** */ (Correct answer)
- Hash comments with #
- Multi-line strings
Correct answer: DocBlocks with /** */
DocBlocks are structured comments beginning with /** that contain annotations like @param, @return, and @throws used by documentation generators.
Question 3: When researching PHP performance between two approaches, what is a key best practice to ensure benchmark results are statistically valid?
- Run each approach only once and compare
- Warm up the environment and run multiple iterations averaging results (Correct answer)
- Use different PHP versions per approach
- Test on local machine only without warmup
Correct answer: Warm up the environment and run multiple iterations averaging results
Warming up the environment and averaging multiple iterations removes outliers caused by JIT compilation, caching, and system variance.
Question 4: Which PHP function, added in PHP 8.0, allows developers to test nullable and chained property accesses safely, reducing the need for verbose null checks in research code?
- isset() chaining
- The nullsafe operator (?->) (Correct answer)
- null_coalesce() function
- Optional::of() pattern
Correct answer: The nullsafe operator (?->)
The nullsafe operator (?->) short-circuits the entire chain and returns null if any link in the chain is null, added in PHP 8.0.
Question 5: In PHP test-driven development (TDD), what does the 'red-green-refactor' cycle mean in practice?
- Alternate between PHP versions during testing
- Write a failing test, make it pass with minimal code, then improve the code (Correct answer)
- Run tests on red servers first, then green servers
- Use error_reporting levels in sequence
Correct answer: Write a failing test, make it pass with minimal code, then improve the code
TDD's red-green-refactor cycle means writing a failing test (red), writing just enough code to pass it (green), then cleaning up the code (refactor).
Question 6: Which Composer command allows a PHP developer to research what packages depend on a specific package within a project?
- composer depends <package>
- composer why <package> (Correct answer)
- composer info --dependents <package>
- composer inspect <package>
Correct answer: composer why <package>
The 'composer why' command shows which installed packages require a specific dependency, helping trace dependency chains.
Question 7: When examining PHP source code of installed libraries for research, which directory under a project root contains the vendor packages?
- /lib/
- /vendor/ (Correct answer)
- /packages/
- /modules/
Correct answer: /vendor/
Composer installs all third-party dependencies into the /vendor/ directory at the project root by default.
Which PHP configuration directive, when enabled, reveals all errors including notices and warnings, which is essential during research and development phases?