PHP Error Handling Levels Quiz

PHP
0 Passed
0% acceptance

A 35-question quiz that demystifies PHP error reporting levels, display/log configuration, custom handlers, and production-safe diagnostics with 11 practical code checks.

35 Questions
~70 minutes
1

Question 1

Why do production stacks usually set display_errors=Off even though development machines keep it On?

A
Hiding errors in production prevents leaking stack traces or secrets to end users while still logging them server-side.
B
display_errors Off makes PHP run faster automatically.
C
Browsers refuse to render output when errors are displayed.
D
Turning it Off disables logging.
2

Question 2

What does this snippet echo?

php
<?php
      error_reporting(E_ERROR | E_PARSE);
      echo (error_reporting() & E_WARNING) ? 'warn' : 'quiet';
      ?>
A
quiet
B
warn
C
E_WARNING
D
Notice
3

Question 3

Why does error_reporting(E_ALL) remain a recommended baseline even when a framework filters logs?

A
E_ALL ensures all native notices, warnings, and deprecations are available; the framework can then decide what to surface or suppress.
B
E_ALL disables custom handlers.
C
Frameworks cannot change logging behavior.
D
E_ALL encrypts logs.
4

Question 4

A DevOps engineer toggles error_reporting via ini_set inside bootstrap code. Why add a comment referencing php.ini as well?

A
Documenting the ini baseline prevents future editors from thinking the runtime override alone controls behavior across environments.
B
Comments change the mask.
C
php.ini cannot be edited otherwise.
D
ini_set only works inside comments.
5

Question 5

What does this snippet print?

php
<?php
      set_error_handler(function () {
          echo 'handled';
      });
      trigger_error('test', E_USER_WARNING);
      ?>
A
handled
B
test
C
Warning: test
D
Notice
6

Question 6

Why should error_log messages include request IDs or correlation tokens?

A
Correlation IDs make it easier to trace a single request across multiple services and log lines.
B
Without IDs, PHP refuses to write logs.
C
IDs encrypt the message.
D
Request IDs disable rate limiting.
7

Question 7

What does this snippet output when display_errors is On?

php
<?php
      set_error_handler(function ($level, $message) {
          echo strtoupper($message);
      });
      $value = $undefined + 1;
      ?>
A
UNDEFINED VARIABLE
B
Notice: Undefined variable
C
1
D
handled
8

Question 8

Why does the @ operator hide errors only if they occur in the same expression?

A
The suppression operator temporarily sets error_reporting to zero for that expression, so nested calls still emit if they run later.
B
@ rewrites code at compile time.
C
Once an error is suppressed, no future error can appear.
D
@ encrypts the output.
9

Question 9

What does this configuration snippet echo?

php
<?php
      ini_set('log_errors', '1');
      ini_set('error_log', __DIR__ . '/var/app.log');
      echo ini_get('log_errors');
      ?>
A
1
B
0
C
var/app.log
D
Notice
10

Question 10

Why do SRE teams centralize PHP error logs via syslog or log shipping agents?

A
Centralization prevents log loss when containers rotate and lets alerting systems watch the aggregated stream.
B
PHP cannot write local files.
C
Syslog automatically fixes code.
D
Agents disable file logging.
11

Question 11

What does this snippet output?

php
<?php 
 error_reporting(E_ALL & ~E_DEPRECATED);
      echo (error_reporting() & E_DEPRECATED) ? 'on' : 'off';
?>
A
off
B
on
C
E_DEPRECATED
D
Notice
12

Question 12

Why should applications escalate E_USER_ERROR events cautiously?

A
E_USER_ERROR halts script execution, so triggering it unnecessarily can interrupt user flows that might otherwise degrade gracefully.
B
E_USER_ERROR encrypts stack traces.
C
It cannot be logged.
D
Browsers require E_USER_ERROR for AJAX.
13

Question 13

What does this snippet echo?

php
<?php
      $previous = set_error_handler(function () {
          echo 'first';
      });
      set_error_handler(function () {
          echo 'second';
      });
      restore_error_handler();
      trigger_error('oops', E_USER_NOTICE);
      ?>
A
first
B
second
C
oops
D
Notice
14

Question 14

Why do QA environments often mirror production error_reporting masks but keep display_errors On?

A
Matching the mask surfaces the same events production would emit, while display_errors On helps testers see issues immediately without tailing logs.
B
QA cannot write logs.
C
display_errors On disables exceptions.
D
Error masks cannot differ between environments.
15

Question 15

What does this snippet output?

php
<?php
      register_shutdown_function(function () {
          $error = error_get_last();
          echo $error ? $error['type'] : 'clean';
      });
      strpos();
      ?>
A
2
B
clean
C
Notice
D
Warning: strpos() expects at least 2 parameters
16

Question 16

Why do teams categorize E_DEPRECATED separately from E_WARNING?

A
Deprecations indicate future compatibility problems rather than immediate runtime failures, so they often route to separate dashboards.
B
E_DEPRECATED cannot be logged.
C
E_WARNING always crashes scripts.
D
Deprecations are invisible to loggers.
17

Question 17

What does this snippet echo?

php
<?php
      ini_set('error_prepend_string', '<strong>');
      ini_set('error_append_string', '</strong>');
      trigger_error('formatted', E_USER_WARNING);
      ?>
A
<strong>Warning: formatted</strong>
B
Warning: formatted
C
formatted
D
Notice
18

Question 18

Why should log rotation consider PHP-FPM pool restarts?

A
Rotating logs without signaling PHP-FPM can leave workers writing to deleted files, so restarts or reopen signals ensure continued logging.
B
PHP-FPM refuses to log otherwise.
C
Restarts encrypt log data.
D
Rotation automatically reloads pools.
19

Question 19

What does this snippet emit?

php
<?php
      try {
          set_error_handler(function ($errno, $errstr) {
              throw new ErrorException($errstr, 0, $errno);
          });
          $x = $undefinedVar + 1;
      } catch (Throwable $e) {
          echo $e->getMessage();
      }
      ?>
A
Undefined variable
B
1
C
handled
D
Notice
20

Question 20

Why do incident reports include the exact error level along with stack trace excerpts?

A
Knowing whether the event was a warning, notice, or fatal guides remediation priority and root-cause analysis.
B
Stack traces already contain levels.
C
Levels encrypt traces.
D
Executives require the numeric code.
21

Question 21

What does this snippet output?

php
<?php
      error_reporting(E_ALL);
      set_error_handler(function ($errno) {
          if ($errno === E_NOTICE) {
              echo 'notice';
              return true;
          }
      });
      $value = $undefined + 1;
      echo 'done';
      ?>
A
noticedone
B
done
C
Notice: Undefined variable
D
Notice
22

Question 22

Why do platform teams audit php.ini for xdebug.default_enable before promoting builds?

A
Leaving Xdebug enabled in production drastically slows requests and may reveal stack details.
B
Xdebug disables error logging.
C
Xdebug encrypts memory.
D
Builds fail without it.
23

Question 23

What does this snippet echo?

php
<?php
      error_reporting(0);
      echo (int) error_reporting();
      ?>
A
0
B
E_ALL
C
1
D
Notice
24

Question 24

Why is logging E_USER_NOTICE helpful even if the application continues functioning?

A
User-generated notices often capture domain-specific edge cases that indicate configuration drift or input problems worth reviewing.
B
Notice logging is required for TLS.
C
Without logging, notices turn into fatals.
D
Users cannot notice issues otherwise.
25

Question 25

Why would a custom error handler return false after logging a notice?

A
Returning false hands control back to PHP so the native handler still emits the standard notice output.
B
Returning false disables logging for the rest of the request.
C
Returning false reduces CPU usage dramatically.
D
PHP requires false so execution can resume at the same line.
26

Question 26

Why do code reviews flag uses of die($message) in library code?

A
Abruptly terminating inside shared libraries prevents callers from handling errors gracefully and may leak UI text.
B
die() cannot output strings.
C
die() disables logging.
D
Library code cannot call functions.
27

Question 27

Why do bootstrap scripts register set_exception_handler in addition to set_error_handler?

A
Exceptions bypass set_error_handler, so a dedicated exception handler catches uncaught exceptions before PHP emits a fatal.
B
set_exception_handler shortens stack traces for warnings.
C
It disables error_log so files stay small.
D
It is required only for CLI scripts.
28

Question 28

Why should display_startup_errors stay Off in production even if logs capture startup failures?

A
Startup errors occur before frameworks sanitize output, so displaying them can leak server paths and credentials to end users.
B
Turning it On disables opcache entirely.
C
Startup errors only happen on Windows hosts.
D
The directive affects JSON APIs only.
29

Question 29

What makes E_RECOVERABLE_ERROR different from E_ERROR?

A
Recoverable errors can be intercepted by custom handlers or converted to exceptions, giving code a chance to clean up before execution stops.
B
E_RECOVERABLE_ERROR is ignored automatically.
C
E_ERROR never appears in logs.
D
Recoverable errors only exist in CLI mode.
30

Question 30

Why might teams raise log_errors_max_len when sending stack traces to syslog?

A
Long traces are truncated by default, so increasing the limit preserves full context for centralized debugging.
B
A larger limit encrypts log traffic.
C
syslog refuses short messages.
D
Raising the limit disables log buffering.
31

Question 31

Why call error_clear_last() after inspecting error_get_last() inside cleanup code?

A
Clearing the last error prevents unrelated shutdown handlers from reacting to a stale warning that was already handled.
B
It speeds up garbage collection.
C
It disables future errors.
D
It is required before every echo.
32

Question 32

Why do CLI operators sometimes pass -derror_reporting when running maintenance scripts?

A
The flag enables temporary verbosity without modifying shared php.ini files used by web traffic.
B
CLI ignores php.ini entirely.
C
The flag encrypts console output.
D
Maintenance scripts cannot log otherwise.
33

Question 33

Why keep E_USER_WARNING metrics separate from E_WARNING metrics?

A
User warnings are triggered intentionally by application code, so separating them from engine warnings preserves signal about business rules.
B
E_USER_WARNING cannot be logged.
C
E_WARNING never includes stack traces.
D
Dashboards cannot chart both levels.
34

Question 34

Why do API responses leave html_errors Off even in staging?

A
HTML markup inside errors corrupts JSON or XML payloads and could enable reflected HTML injection for clients.
B
APIs cannot log while html_errors is On.
C
html_errors is required only for POST requests.
D
Turning it Off disables templating.
35

Question 35

Why do runbooks document the expected error_reporting value for each environment?

A
Shared guidance keeps new teammates from promoting builds with mismatched masks and makes local reproduction straightforward.
B
Runbooks automatically change php.ini.
C
Without documentation, PHP forgets the value every hour.
D
The mask can only be set once per project.

QUIZZES IN PHP