Error Handling in PHP Flashcards

1
Q

Q: What is error handling in PHP?

A

A: The process of catching and managing errors to prevent application crashes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: What are the types of errors in PHP?

A

A: Notices, Warnings, Fatal Errors, and Parse Errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: What function is used to trigger a user-defined error in PHP?

A

A: trigger_error().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: What is the default error reporting level in PHP?

A

A: E_ALL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: How do you enable error reporting in PHP?

A

ini_set(‘display_errors’, 1);
error_reporting(E_ALL);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: What does E_NOTICE represent in PHP?

A

A: Non-critical runtime errors, like using undefined variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: What does E_WARNING represent in PHP?

A

A: Runtime errors that do not halt script execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: What does E_ERROR represent in PHP?

A

A: Critical runtime errors that halt script execution.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: What does E_PARSE represent in PHP?

A

A: Errors that occur during code parsing, such as syntax errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: What does E_ALL represent in PHP?

A

A: Reports all PHP errors except E_STRICT prior to PHP 5.4.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: What function retrieves the last error that occurred in PHP?

A

A: error_get_last().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: What function logs errors to a file in PHP?

A

A: error_log().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: How do you set a custom error handler in PHP?

A

A: Use the set_error_handler() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: How do you restore the previous error handler in PHP?

A

A: Use the restore_error_handler() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: How do you set a shutdown function to handle fatal errors?

A

A: Use the register_shutdown_function() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: What is the syntax for a custom error handler in PHP?

A

function customError($errno, $errstr, $errfile, $errline) {
// Handle the error
}
set_error_handler(‘customError’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q: What are the parameters of a custom error handler?

A

A: Error number, error message, error file, and error line.

18
Q

Q: Can you suppress errors in PHP?

A

A: Yes, using the @ error control operator.

19
Q

Q: Why should you avoid using the @ operator?

A

A: It hides all errors, making debugging difficult.

20
Q

Q: What does set_error_handler() return?

A

A: The previous error handler if successful, or NULL on failure.

21
Q

Q: What is an exception in PHP?

A

A: A way to handle errors and special conditions using try-catch blocks.

22
Q

Q: What keyword is used to throw an exception?

A

A: throw.

23
Q

Q: How do you define a try-catch block in PHP?

A

try {
// Code that may throw an exception
} catch (Exception $e) {
// Handle the exception
}

24
Q

Q: What method retrieves the message from an exception object?

A

A: $e->getMessage().

25
Q

Q: What is the purpose of the finally block in PHP?

A

A: To execute code regardless of whether an exception was thrown or not.

26
Q

Q: How do you create a custom exception class in PHP?

A

A: Extend the Exception class:

class MyException extends Exception {
// Custom methods or properties
}

27
Q

Q: Can you catch multiple exceptions in a single try block?

A

A: Yes, by using multiple catch blocks.

28
Q

Q: What is the getCode() method of an exception?

A

A: Retrieves the exception’s error code.

29
Q

Q: What is the getFile() method of an exception?

A

A: Retrieves the file where the exception was thrown.

30
Q

Q: What is the getLine() method of an exception?

A

A: Retrieves the line number where the exception was thrown.

31
Q

Q: How do you enable error logging in PHP?

A

ini_set(‘log_errors’, 1);
ini_set(‘error_log’, ‘/path/to/logfile.log’);

32
Q

Q: What does error_log() do in PHP?

A

A: Sends an error message to the defined log file, email, or system log.

33
Q

Q: How can you set a custom error log file in php.ini?

A

A: error_log = /path/to/logfile.log.

34
Q

Q: What is the default location for PHP error logs?

A

A: It varies by server configuration but is typically in the web server’s error log.

35
Q

Q: How can you log errors to an email in PHP?

A

A: Use the error_log() function with the 3 parameter and a mail handler.

36
Q

Q: Why should you not display errors in a production environment?

A

A: It exposes sensitive information to users.

37
Q

Q: How can you disable error display in PHP?

A

ini_set(‘display_errors’, 0);

38
Q

Q: Why is it important to use error logging?

A

A: To track and debug issues without exposing them to end-users.

39
Q

Q: How do you test error handling in development?

A

A: Enable full error reporting using error_reporting(E_ALL).

40
Q

Q: What is the difference between die() and exceptions for handling errors?

A

A: die() stops script execution immediately, while exceptions allow controlled error handling.