What is php error message?
Error handling in PHP is simple. An error message with filename, line number and a message describing the error is sent to the browser. Show PHP Error HandlingWhen creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. This tutorial contains some of the most common error checking methods in PHP. We will show different error handling methods:
Basic Error Handling: Using the die() functionThe first example shows a simple script that opens a text file: If the file does not exist you might get an error like this: Warning: fopen(mytestfile.txt) [function.fopen]: failed to open stream: To prevent the user from getting an error message like the one above, we test whether the file exist before we try to access it: Example if(file_exists("mytestfile.txt")) { Try it Yourself » Now if the file does not exist you get an error like this: Error: The file does not exist. The code above is more efficient than the earlier code, because it uses a simple error handling mechanism to stop the script after the error. However, simply stopping the script is not always the right way to go. Let's take a look at alternative PHP functions for handling errors. Creating a Custom Error HandlerCreating a custom error handler is quite simple. We simply create a special function that can be called when an error occurs in PHP. This function must be able to handle a minimum of two parameters (error level and error message) but can accept up to five parameters (optionally: file, line-number, and the error context): Syntax error_function(error_level,error_message,
Error Report levelsThese error report levels are the different types of error the user-defined error handler can be used for:
Now lets create a function to handle errors: function customError($errno, $errstr) { The code above is a simple error handling function. When it is triggered, it gets the error level and an error message. It then outputs the error level and message and terminates the script. Now that we have created an error handling function we need to decide when it should be triggered. Set Error HandlerThe default error handler for PHP is the built in error handler. We are going to make the function above the default error handler for the duration of the script. It is possible to change the error handler to apply for only some errors, that way the script can handle different errors in different ways. However, in this example we are going to use our custom error handler for all errors: set_error_handler("customError"); Since we want our custom function to handle all errors, the ExampleTesting the error handler by trying to output variable that does not exist: //error handler function //set error handler //trigger error The output of the code above should be something like this: Error: [8] Undefined variable: test Trigger an ErrorIn a script where users can input data it is useful to trigger errors when an illegal input occurs. In PHP, this is done by the ExampleIn this example an error occurs if the "test" variable is bigger than "1": $test=2; The output of the code above should be something like this: Notice: Value must be 1 or below An error can be triggered anywhere you wish in a script, and by adding a second parameter, you can specify what error level is triggered. Possible error types:
ExampleIn this example an E_USER_WARNING occurs if the "test" variable is bigger than "1". If an E_USER_WARNING occurs we will use our custom error handler and end the script: //error handler function //set error handler //trigger error The output of the code above should be something like this: Error: [512] Value must be 1 or below Now that we have learned to create our own errors and how to trigger them, lets take a look at error logging. Error LoggingBy default, PHP sends an error log to the server's logging system or a file, depending on how the error_log
configuration is set in the php.ini file. By using the Sending error messages to yourself by e-mail can be a good way of getting notified of specific errors. Send an Error Message by E-MailIn the example below we will send an e-mail with an error message and end the script, if a specific error occurs: //error handler function //set error handler //trigger error The output of the code above should be something like this: Error: [512] Value must be 1 or below And the mail received from the code above looks like this: Error: [512] Value must be 1 or below This should not be used with all errors. Regular errors should be logged on the server using the default PHP logging system. How do I fix PHP errors?Editing the php.. Log into your cPanel.. Go to the File Manager. ... . Find the “Error handling and logging” section in the php.ini. ... . Next you can set the display_errors variable to On or Off to either show the errors on your website or not.. What are types of errors in PHP?In PHP, mainly four types of errors are considered:. Syntax Error or Parse Error.. Fatal Error.. Warning Error.. Notice Error.. What is a PHP notice?PHP notices are “soft errors” in PHP programming language. If you enable error logging (debug) in php. ini configuration file, they will pop-up in your logs from time-to-time during various development stages.
What causes PHP fatal error?PHP Fatal Errors are coding errors so severe that the server cannot display the site properly - they are usually caused by conflicting or poorly designed extensions, . htaccess misconfigurations and things like that.
|