PHP Professional Standards & Competencies 2 — Questions and Answers
Question 1: Which PSR standard defines the basic coding style guide for PHP (indentation, line length, etc.)?
- PSR-1
- PSR-2 (Correct answer)
- PSR-4
- PSR-12
Correct answer: PSR-2
PSR-2 is the coding style guide that extends PSR-1 with rules for indentation, braces, spaces, and line length.
Question 2: What is the purpose of a `.editorconfig` file in a PHP project?
- Define autoloading rules
- Enforce consistent editor settings across different editors and IDEs (Correct answer)
- Configure PHP-FPM worker pools
- Manage Composer dependencies
Correct answer: Enforce consistent editor settings across different editors and IDEs
An `.editorconfig` file specifies formatting rules (indent style, charset, line endings) that compatible editors automatically apply.
Question 3: When writing a library intended for public consumption, which versioning strategy should you follow for its releases?
- Use date-based versioning (e.g., 2024.01.15)
- Follow Semantic Versioning (SemVer) with MAJOR.MINOR.PATCH (Correct answer)
- Increment a single integer for every release
- Use random build hashes as versions
Correct answer: Follow Semantic Versioning (SemVer) with MAJOR.MINOR.PATCH
Semantic Versioning communicates breaking changes (MAJOR), new features (MINOR), and bug fixes (PATCH) clearly to downstream consumers.
Question 4: Which tool is the de facto standard for static analysis of PHP code to catch type errors and potential bugs before runtime?
- PHP_CodeSniffer
- PHPStan (Correct answer)
- Xdebug
- Blackfire
Correct answer: PHPStan
PHPStan performs static analysis at configurable strictness levels (0–9) to find bugs without executing the code.
Question 5: In a professional PHP project, where should database credentials and API keys be stored?
- Hardcoded in the source files for easy access
- In environment variables or a `.env` file excluded from version control (Correct answer)
- In a public config file committed to the repo
- In a comment block at the top of the main script
Correct answer: In environment variables or a `.env` file excluded from version control
Secrets must be kept out of source control; environment variables or a local `.env` file (gitignored) are the standard approach.
Question 6: What does the principle of 'fail fast' mean in the context of professional PHP development?
- Write code that executes as quickly as possible
- Detect and report errors at the earliest possible point rather than propagating invalid state (Correct answer)
- Deploy to production immediately after writing code
- Skip validation to reduce latency
Correct answer: Detect and report errors at the earliest possible point rather than propagating invalid state
Failing fast means throwing exceptions or asserting conditions early so bugs surface close to their source rather than causing silent corruption downstream.
Question 7: Which practice is considered a professional standard when handling exceptions in PHP?
- Catch all exceptions with a bare `catch (Exception $e) {}` and ignore them
- Log the exception with context, then either rethrow or return an appropriate error response (Correct answer)
- Use `die()` to terminate the script on any exception
- Convert all exceptions to PHP notices
Correct answer: Log the exception with context, then either rethrow or return an appropriate error response
Professional exception handling involves logging sufficient context for debugging and responding gracefully rather than silently swallowing or abruptly terminating.
Which PSR standard defines the basic coding style guide for PHP (indentation, line length, etc.)?