PHP error handling

| | | | | | | | | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

PHP error handling methods:
  • Using the die() method
  • Custom error handling
Basic error handling: using the die() function Thedie() function prints a message and exits the current script.Syntax:
 die ($message)
Example :
// PHP code showing default error handling $file = fopen ( "engineer.txt" , "w" ); ?>
Note.Run the above code and the engineer.txt file is missing, and then a runtime error will be displayed.
Runtime Error:
PHP Warning: fopen (engineer.txt): failed to open stream: Permission denied in /home/dac923dff0a2558b37ba742613273073.php on line 2
To prevent this error, use the die() function. The die() function is implemented below:Example :
// PHP code to check for errors
// if there is no file
// then exit script if (! file_exists ( "engineer.txt" )) { die ( " File is not present " ); }
// if file is present
// then continue else { $file = fopen ( " engineer.txt " , " w " ); }
?>
Note.If the file is engineer.txt is missing, it will display output.
Exit
File is not present
Custom error handlers:Creating a custom error handler in PHP is very easy. Create a function that can be called when an error occurs in PHP.Syntax:
error_function ($error_level, $error_message, $error_file, $error_line, $error_context)
Parameters:This function takes five parameters as above and described below:
  • $error_level: This is a required parameter and must be an integer. There are predefined error levels.
  • $error_message:This is a required parameter and this is the message the user wants to print.
  • $error_file: is an optional parameter used to specify the file where the error occurred.
  • $error_line:is an optional parameter used to specify the line number where the error occurred.
  • $error_context:this is an optional parameter that is used to specify an array containing each variable and their value when an error occurs.
error_level :these are the possible error levels, which are listed below:
  • 1: .E_ERROR: fatal run-time error was stopped
  • 2: E_WARNING: non-fatal runtime error, script execution stopped
  • 4: E_PARSE: compile-time error generated by the parser
  • 8: E_NOT ICE: the script encountered something that could be an error
  • 16: E_CORE_ERROR: fatal errors that occurred during the initial run of the script
  • 32: E_CORE_WARNING: non-fatal errors that occurred during the initial script run
  • 8191: E_ALL: all errors and warnings
set_error_handler() function:After creating the myerror() function, you need to install a custom handler errors because PHP normally handles it, but if the user is doing custom error handling then the user should set it instead of an argument and pass the myerror function as a string.Example:
// Create my error function that prints the message
// user function myerror ( $error_no , $error_msg ) { echo "Error: [$error_no] $error_msg" ; echo "Now Script will end " ; // If an error occurs, the script must be stopped die (); }
// Setting set_error_handler set_error_handler ( "myerror" ); $a = 10; $b = 0;
// This will generate an error echo ( $a / $b ) ;; ?>
Output:
Error: [2] Division by zero Now Script will end
Output:always try to handle errors using custom error handling because it will show a more specific message in according to the user, which can be useful to the user. If the error is not handled with custom error handling, then an error occurs, then the script will be stopped by default, but if it handles the error with custom error handling, it can continue the script after displaying the error message.

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from PHP error handling, check other array Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Schneider Krasiko

Rome | 2023-03-30

Maybe there are another answers? What PHP error handling exactly means?. Will use it in my bachelor thesis

Javier Chamberlet

Abu Dhabi | 2023-03-30

os Python module is always a bit confusing 😭 PHP error handling is not the only problem I encountered. Checked yesterday, it works!

Manuel Galleotti

San Francisco | 2023-03-30

I was preparing for my coding interview, thanks for clarifying this - PHP error handling in Python is not the simplest one. Checked yesterday, it works!

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically