Friday 10, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

Error Handling

Error handling is the process of changing the control flow of a program in response to error conditions. Error conditions can be caused by a variety of factors - programmer error, corrupt input data, software requirements deficiencies and in many other conditions.
A web application needs to be able to gracefully handle all of these potential problems - recovering from them where possible and exiting gracefully when the error is fatal.
We will show different error handling methods:
  1. Simple "die()" statements
  2. Custom errors and error triggers
  3. Error reporting

 

Simple die() statement:
if(!file_exists("myfile.txt"))
 {
 die("File not found");
 }
else
 {
 $file=fopen("myfile.txt","r");
 }

?>

 

If the file does not exist you get an error message like this:

File not found

 

Custom errors and error triggers

You can create a custom error handler in PHP to replace the standard output to the browser. First you set up the error handler to replace the normal one, and set the ini values.

 

ini_set('display_errors', 'Off');

ini_set('log_errors', 'On');

define('ERROR_LOG_PATH', 'error_log.txt');

set_error_handler("custom_err_handler");

 

The custom function is defined we must then create it. The function takes 4 arguments.
  1. $num: level of the error raised
  2. $str: contains the error message
  3. $file: filename that the error was raised in
  4. $line: line number the error was raised at

 

function custom_err_handler($num, $str, $file, $line) {

    print "Error Handler Called!";

    $err = "";

    $err .= "PHP Error\n";

    $err .= "Number: [" . $num . "]\n";

    $err .= "String: [" . $str . "]\n";

    $err .= "File: [" . $file . "]\n";

    $err .= "Line: [" . $line . "]\n\n";

    error_log($err, 3, ERROR_LOG_PATH);

}

 

function Errfunction() {

            trigger_error("errror message");

}

 

Now call the Errfunction()

myFunction();

 


 
 
     
   
PHP Tutorial
 
  Introduction
Introduction
  Variables
Variables
  Strings
Strings
  Operators
Operators
  If Else Statement
If Else Statement
  Loops
Loops
  Functions
Functions
  Forms
Forms
  Sessions
Sessions
  Cookies
Cookies
  Files
Files
  E-mail
E-mail
  Error Handling
Error Handling
  File Upload
File Upload
  PHP Databases
PHP Databases
  Connecting to a MySQL Database
Connecting to a MySQL Database
  MySQL Data Types
MySQL Data Types
  Inserting Data Into a Table
Inserting Data Into a Table
  Select Statement
Select Statement
  Where Clause
Where Clause
  Order By
Order By
  Update Statement
Update Statement
  Delete From Statement
Delete From Statement
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download 	JavaScript Essentials Ebook JavaScript Essentials
   
Download The Leading Affiliate Money Machine Ebook The Leading Affiliate Money Machine
   
Download An Introduction to GCC  Ebook An Introduction to GCC
   
Download 4 Surefire Plans To Unleash The Real Potential Of Private Label Rights Ebook 4 Surefire Plans To Unleash The Real Potential Of Private Label Rights
   
Download develop your own database software  Ebook develop your own database software
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz