Repository
https://github.com/php/php-src
What Will I Learn?
- You will learn the die method.
- You will learn how to create a custom error.
- You will learn how to trigger an error.
- You will learn the use of exceptions.
Requirements
System Requirements:
- Server support PHP , Xampp or Wamp for example
- An IDE like Sublime text.
- Browser (Chrome for example)
OS Support for PHP
- Windows
- macOS
- Linux
Required Knowledge
- HTML language
- CSS language
Difficulty
- Intermediate
Description
In this tutorial we will learn the errors, so an error is defined as an anomaly in the application so we can say that there is an error if the program does not work as it was designed.
During the development an error can come for example from the non respect of the syntax of the language, a bad configuration.. etc.
It can also be an error in the algorithm itself, so our code can generate for example " negative value prices or something like that ", this drives us to be very careful during the design phase.
From error detection and from information to the correction of these errors, Php offers us the possibility to better manage these errors.
Today we will see the " die method, creating a custom error handler, trigger an error and the use of the exceptions ".
1- Die Method
To handle the errors we can use the function " die()
", the word "die" in English that's to say a thing was alive and it blocks it dead, it has no effect now.
If we apply this rule to the errors if we have an error, using the " die()
" function we will display a message and we will stop the script execution.
2- Creating a custom error handler
To create a custom error for an error occurs in your site or your application, PHP offers a very efficient idea that is the " Custom Error Handler ".
To create an error you must use a function that accepts 5 parameters, 2 that are very important, and 3 that are optional.
- The first parameter is the level of error that is a number, we will look at the error levels.
- The second parameter is the message, that is the message that will be displayed in the page for the user.
- The third is the filename or file path where we have the error.
- The fourth is the line where we have the error.
- Finally the context that is an array, we will see an example to understand them.
After that we will use the function "error_set_handler()
" that it accepts the function that we created it to execute this function if there is this error.
The type of an error is represented by a PHP constant.
- Informative message (constant: E_NOTICE)
- Alert message (Constant: E_WARNING)
- Critical Errors (Constant: E_ERROR )
- Syntax errors (Constant E_PARSE)
- PHP internal errors (Constants: E_CORE_ERROR and E_CORE_WARNING)
- Compilation errors (Constants: E_COMPILE_ERROR and E_COMPILE_WARNING)
- User Errors (E_USER_ERROR, E_WARNING, E_NOTICE)
- E_ALL
3- Trigger an error
To trigger an error in your page or application, PHP gives us a " trigger_error()
" function, this function accepts 2 parameters :
- The first parameter is the message that will be displayed.
- The second parameter is the level of error, we have 3 levels:
- E_USER_ERROR.
- E_USER_WARNING.
- E_USER_NOTICE.
We can trigger the error but we must first create the error or specify the error as the example we have already done.
4- Use of exceptions
Currently you know fatal errors, alerts, analysis errors or notices. We will discover a different way to handle errors, We will in a way create our own types of errors.
Exceptions are quite different errors that do not work the same way, as you will see this new way of dealing with errors is quite practical.
To throw the exception we will use the " throw()
" function with the message that will be displayed in the page for the user.
Video Tutorial
Curriculum
- PHP Tutorial #11 String Methods ( Addslaches, Chr, Chop and Chunk_splite )
- PHP Tutorial #12 String Methods ( Bin2Hex, Count_Chars, Explode and Hex2Bin )
- PHP Tutorial #13 String Methods (Implode, Join, Lcfirst, Ltrim and Fprintf )
- PHP Tutorial #14 String Methods (Md5_File, Md5, Nl2br and Addcslaches )
- PHP Tutorial #15 String Methods (Number_Format, Ord, Parse_Str, Print and Printf )
- PHP Tutorial #16 String Methods (QuoteMeta, Rtrim , Sha1_file and Sha1 )
- PHP Tutorial #17 Mathematical Methods (Abs, Acos , Acosh and Asin )
- PHP Tutorial #18 ( GET and POST ) Methods and $_SERVER Variables
- PHP Tutorial #19 ( Include and Require ) , Globals Variable, Request and $_SERVER Variables
- PHP Tutorial #20 The Cookie How To Create , Modify And Delete A Cookie
- PHP Tutorial #21 The Session How To Create , Modify And Delete A Session
- PHP Tutorial #22 The Output_Buffer And How To Control It
- PHP Tutorial #23 Filters Part 1 " FILTER_VALIDATE "
- PHP Tutorial #24 Filters Part 2 " FILTER_SANITIZE "
- PHP Tutorial #25 File System Part 1 " Fopen, Fread, Fwrite and File_get_contents "
- PHP Tutorial #26 File System Part 2 and Filter Part 3
- PHP Tutorial #27 The Date ( timestamp, date, timezone and strtotime )
Proof of Work Done
https://github.com/alexendre-maxim/PHP-Tutorial/blob/master/errors.php