SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Introduction to PHPIntroduction to PHP
Error HandlingError Handling
TypesTypes
There are 12 unique error types, which can
be grouped into 3 main categories:
• Informational (Notices)
• Actionable (Warnings)
• Fatal
Informational ErrorsInformational Errors
• Harmless problem, and can be avoided through
use of explicit programming.
e.g. use of an undefined variable, defining a string
without quotes, etc.
Actionable ErrorsActionable Errors
• Indicate that something clearly wrong has
happened and that action should be taken.
e.g. file not present, database not available,
missing function arguments, etc.
Fatal ErrorsFatal Errors
• Something so terrible has happened during
execution of your script that further
processing simply cannot continue.
e.g. parsing error, calling an undefined
function, etc.
Identifying ErrorsIdentifying Errors
E_STRICT Feature or behaviour is depreciated (PHP5).
E_NOTICE Detection of a situation that could indicate a problem,
but might be normal.
E_USER_NOTICE User triggered notice.
E_WARNING Actionable error occurred during execution.
E_USER_WARNING User triggered warning.
E_COMPILE_WARNING Error occurred during script compilation (unusual)
E_CORE_WARNING Error during initialization of the PHP engine.
E_ERROR Unrecoverable error in PHP code execution.
E_USER_ERROR User triggered fatal error.
E_COMPILE_ERROR Critical error occurred while trying to read script.
E_CORE_ERROR Occurs if PHP engine cannot startup/etc.
E_PARSE Raised during compilation in response to syntax error
notice
warning
fatal
Causing errorsCausing errors
• It is possible to cause PHP at any point in your
script.
trigger_error($msg,$type);
e.g.
…
if (!$db_conn) {
trigger_error(‘db conn
failed’,E_USER_ERROR);
}
…
PHP Error HandlingPHP Error Handling
Customizing ErrorCustomizing Error
HandlingHandling
• Generally, how PHP handles errors is defined
by various constants in the installation (php.ini).
• There are several things you can control in
your scripts however..
Set error reportingSet error reporting
settingssettings
error_reporting($level)
This function can be used to control which
errors are displayed, and which are simply
ignored. The effect only lasts for the duration
of the execution of your script.
Set error reporting settingsSet error reporting settings
<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
error_reporting(E_ALL ^ E_NOTICE);
// Report ALL PHP errors
error_reporting(E_ALL);
?>
Set error reportingSet error reporting
settingssettings
• Hiding errors is NOT a solution to a problem.
• It is useful, however, to hide any errors
produced on a live server.
• While developing and debugging code,
displaying all errors is highly recommended!
Suppressing ErrorsSuppressing Errors
• The special @ operator can be used to
suppress function errors.
• Any error produced by the function is
suppressed and not displayed by PHP
regardless of the error reporting setting.
Suppressing ErrorsSuppressing Errors
$db = @mysql_connect($h,$u,
$p);
if (!$db) {
trigger_error(‘blah’,E_USER_
ERROR);
}
Suppressing ErrorsSuppressing Errors
$db = @mysql_connect($h,$u,$p);
if (!$db) {
trigger_error(blah.',E_USER_ERROR
);
}
$db = @mysql_connect($h,$u,$p);
Attempt to connect to
database. Suppress error
notice if it fails..
Suppressing ErrorsSuppressing Errors
$db = @mysql_connect($h,$u,$p);
if (!$db) {
trigger_error(‘blah’,E_USER_ERROR);
}
Since error is suppressed, it
must be handled gracefully
somewhere else..
Suppressing ErrorsSuppressing Errors
• Error suppression is NOT a solution to a
problem.
• It can be useful to locally define your own
error handling mechanisms.
• If you suppress any errors, you must check
for them yourself elsewhere.
Custom Error HandlerCustom Error Handler
• You can write your own function to handle
PHP errors in any way you want.
• You simply need to write a function with
appropriate inputs, then register it in your
script as the error handler.
• The handler function should be able to
receive 4 arguments, and return true to
indicate it has handled the error…
Custom Error HandlerCustom Error Handler
function err_handler(
$errcode,$errmsg,$file,$lineno) {
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
Custom Error HandlerCustom Error Handler
function err_handler(
$errcode,$errmsg,$file,$lineno) {
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
$errcode,$errmsg,$file,$lineno) {
The handler must have 4 inputs..
1.error code
2.error message
3.file where error occurred
4.line at which error occurred
Custom Error HandlerCustom Error Handler
function err_handler(
$errcode,$errmsg,$file,$lineno) {
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
Any PHP statements can be
executed…
Custom Error HandlerCustom Error Handler
function err_handler(
$errcode,$errmsg,$file,$lineno) {
echo ‘An error has occurred!<br />’;
echo “file: $file<br />”;
echo “line: $lineno<br />”;
echo “Problem: $errmsg”;
return true;
}
return true;
Return true to let PHP know
that the custom error handler
has handled the error OK.
Custom Error HandlerCustom Error Handler
• The function then needs to be registered as your
custom error handler:
set_error_handler(‘err_handler’);
• You can ‘mask’ the custom error handler so it
only receives certain types of error. e.g. to
register a custom handler just for user triggered
errors:
set_error_handler(‘err_handler’,
E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR);
Custom Error HandlerCustom Error Handler
• A custom error handler is never passed
E_PARSE, E_CORE_ERROR or
E_COMPILE_ERROR errors as these are
considered too dangerous.
• Often used in conjunction with a ‘debug’
flag for neat combination of debug and
production code display..
ThankThank You !!!You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/php-classes-in-mumbai.html

Weitere ähnliche Inhalte

Was ist angesagt?

Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
Varun C M
 

Was ist angesagt? (20)

Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
9. ES6 | Let And Const | TypeScript | JavaScript
9. ES6 | Let And Const | TypeScript | JavaScript9. ES6 | Let And Const | TypeScript | JavaScript
9. ES6 | Let And Const | TypeScript | JavaScript
 
Jquery
JqueryJquery
Jquery
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
This keyword and final keyword
This keyword and final  keywordThis keyword and final  keyword
This keyword and final keyword
 
Php array
Php arrayPhp array
Php array
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
OOP Assignment 03.pdf
OOP Assignment 03.pdfOOP Assignment 03.pdf
OOP Assignment 03.pdf
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
Android intents
Android intentsAndroid intents
Android intents
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handling
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 

Andere mochten auch (6)

Exceptions in PHP
Exceptions in PHPExceptions in PHP
Exceptions in PHP
 
Php exceptions
Php exceptionsPhp exceptions
Php exceptions
 
Errors, Exceptions & Logging (PHP Hants Oct '13)
Errors, Exceptions & Logging (PHP Hants Oct '13)Errors, Exceptions & Logging (PHP Hants Oct '13)
Errors, Exceptions & Logging (PHP Hants Oct '13)
 
Elegant Ways of Handling PHP Errors and Exceptions
Elegant Ways of Handling PHP Errors and ExceptionsElegant Ways of Handling PHP Errors and Exceptions
Elegant Ways of Handling PHP Errors and Exceptions
 
Use of Monolog with PHP
Use of Monolog with PHPUse of Monolog with PHP
Use of Monolog with PHP
 
Handling error & exception in php
Handling error & exception in phpHandling error & exception in php
Handling error & exception in php
 

Ähnlich wie PHP - Introduction to PHP Error Handling

Error handling and debugging
Error handling and debuggingError handling and debugging
Error handling and debugging
salissal
 
Error handling in XPages
Error handling in XPagesError handling in XPages
Error handling in XPages
dominion
 

Ähnlich wie PHP - Introduction to PHP Error Handling (20)

Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptx
 
Introduction to php exception and error management
Introduction to php  exception and error managementIntroduction to php  exception and error management
Introduction to php exception and error management
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
 
Error handling
Error handlingError handling
Error handling
 
Getting modern with logging via log4perl
Getting modern with logging via log4perlGetting modern with logging via log4perl
Getting modern with logging via log4perl
 
Sending emails through PHP
Sending emails through PHPSending emails through PHP
Sending emails through PHP
 
TDC 2015 - POA - Trilha PHP - Shit Happens
TDC 2015 - POA - Trilha PHP - Shit HappensTDC 2015 - POA - Trilha PHP - Shit Happens
TDC 2015 - POA - Trilha PHP - Shit Happens
 
Error and Exception Handling in PHP
Error and Exception Handling in PHPError and Exception Handling in PHP
Error and Exception Handling in PHP
 
Error handling and debugging
Error handling and debuggingError handling and debugging
Error handling and debugging
 
Session10-PHP Misconfiguration
Session10-PHP MisconfigurationSession10-PHP Misconfiguration
Session10-PHP Misconfiguration
 
PerlScripting
PerlScriptingPerlScripting
PerlScripting
 
Module-4_WTA_PHP Class & Error Handling
Module-4_WTA_PHP Class & Error HandlingModule-4_WTA_PHP Class & Error Handling
Module-4_WTA_PHP Class & Error Handling
 
Php
PhpPhp
Php
 
Error handling in XPages
Error handling in XPagesError handling in XPages
Error handling in XPages
 
ELMAH
ELMAHELMAH
ELMAH
 
Debugging With Php
Debugging With PhpDebugging With Php
Debugging With Php
 
Debugging
DebuggingDebugging
Debugging
 
Errors
ErrorsErrors
Errors
 
Exception handling in asp.net
Exception handling in asp.netException handling in asp.net
Exception handling in asp.net
 

Mehr von Vibrant Technologies & Computers

Mehr von Vibrant Technologies & Computers (20)

Buisness analyst business analysis overview ppt 5
Buisness analyst business analysis overview ppt 5Buisness analyst business analysis overview ppt 5
Buisness analyst business analysis overview ppt 5
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
 
SQL- Introduction to SQL database
SQL- Introduction to SQL database SQL- Introduction to SQL database
SQL- Introduction to SQL database
 
ITIL - introduction to ITIL
ITIL - introduction to ITILITIL - introduction to ITIL
ITIL - introduction to ITIL
 
Salesforce - Introduction to Security & Access
Salesforce -  Introduction to Security & Access Salesforce -  Introduction to Security & Access
Salesforce - Introduction to Security & Access
 
Data ware housing- Introduction to olap .
Data ware housing- Introduction to  olap .Data ware housing- Introduction to  olap .
Data ware housing- Introduction to olap .
 
Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.
 
Data ware housing- Introduction to data ware housing
Data ware housing- Introduction to data ware housingData ware housing- Introduction to data ware housing
Data ware housing- Introduction to data ware housing
 
Salesforce - classification of cloud computing
Salesforce - classification of cloud computingSalesforce - classification of cloud computing
Salesforce - classification of cloud computing
 
Salesforce - cloud computing fundamental
Salesforce - cloud computing fundamentalSalesforce - cloud computing fundamental
Salesforce - cloud computing fundamental
 
SQL- Introduction to PL/SQL
SQL- Introduction to  PL/SQLSQL- Introduction to  PL/SQL
SQL- Introduction to PL/SQL
 
SQL- Introduction to advanced sql concepts
SQL- Introduction to  advanced sql conceptsSQL- Introduction to  advanced sql concepts
SQL- Introduction to advanced sql concepts
 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data   SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
 
SQL- Introduction to SQL Set Operations
SQL- Introduction to SQL Set OperationsSQL- Introduction to SQL Set Operations
SQL- Introduction to SQL Set Operations
 
Sas - Introduction to designing the data mart
Sas - Introduction to designing the data martSas - Introduction to designing the data mart
Sas - Introduction to designing the data mart
 
Sas - Introduction to working under change management
Sas - Introduction to working under change managementSas - Introduction to working under change management
Sas - Introduction to working under change management
 
SAS - overview of SAS
SAS - overview of SASSAS - overview of SAS
SAS - overview of SAS
 
Teradata - Architecture of Teradata
Teradata - Architecture of TeradataTeradata - Architecture of Teradata
Teradata - Architecture of Teradata
 
Teradata - Restoring Data
Teradata - Restoring Data Teradata - Restoring Data
Teradata - Restoring Data
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

PHP - Introduction to PHP Error Handling

  • 1.
  • 2. Introduction to PHPIntroduction to PHP Error HandlingError Handling
  • 3. TypesTypes There are 12 unique error types, which can be grouped into 3 main categories: • Informational (Notices) • Actionable (Warnings) • Fatal
  • 4. Informational ErrorsInformational Errors • Harmless problem, and can be avoided through use of explicit programming. e.g. use of an undefined variable, defining a string without quotes, etc.
  • 5. Actionable ErrorsActionable Errors • Indicate that something clearly wrong has happened and that action should be taken. e.g. file not present, database not available, missing function arguments, etc.
  • 6. Fatal ErrorsFatal Errors • Something so terrible has happened during execution of your script that further processing simply cannot continue. e.g. parsing error, calling an undefined function, etc.
  • 7. Identifying ErrorsIdentifying Errors E_STRICT Feature or behaviour is depreciated (PHP5). E_NOTICE Detection of a situation that could indicate a problem, but might be normal. E_USER_NOTICE User triggered notice. E_WARNING Actionable error occurred during execution. E_USER_WARNING User triggered warning. E_COMPILE_WARNING Error occurred during script compilation (unusual) E_CORE_WARNING Error during initialization of the PHP engine. E_ERROR Unrecoverable error in PHP code execution. E_USER_ERROR User triggered fatal error. E_COMPILE_ERROR Critical error occurred while trying to read script. E_CORE_ERROR Occurs if PHP engine cannot startup/etc. E_PARSE Raised during compilation in response to syntax error notice warning fatal
  • 8. Causing errorsCausing errors • It is possible to cause PHP at any point in your script. trigger_error($msg,$type); e.g. … if (!$db_conn) { trigger_error(‘db conn failed’,E_USER_ERROR); } …
  • 9. PHP Error HandlingPHP Error Handling
  • 10. Customizing ErrorCustomizing Error HandlingHandling • Generally, how PHP handles errors is defined by various constants in the installation (php.ini). • There are several things you can control in your scripts however..
  • 11. Set error reportingSet error reporting settingssettings error_reporting($level) This function can be used to control which errors are displayed, and which are simply ignored. The effect only lasts for the duration of the execution of your script.
  • 12. Set error reporting settingsSet error reporting settings <?php // Turn off all error reporting error_reporting(0); // Report simple running errors error_reporting(E_ERROR | E_WARNING | E_PARSE); // Reporting E_NOTICE can be good too (to report uninitialized // variables or catch variable name misspellings ...) error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); // Report all errors except E_NOTICE error_reporting(E_ALL ^ E_NOTICE); // Report ALL PHP errors error_reporting(E_ALL); ?>
  • 13. Set error reportingSet error reporting settingssettings • Hiding errors is NOT a solution to a problem. • It is useful, however, to hide any errors produced on a live server. • While developing and debugging code, displaying all errors is highly recommended!
  • 14. Suppressing ErrorsSuppressing Errors • The special @ operator can be used to suppress function errors. • Any error produced by the function is suppressed and not displayed by PHP regardless of the error reporting setting.
  • 15. Suppressing ErrorsSuppressing Errors $db = @mysql_connect($h,$u, $p); if (!$db) { trigger_error(‘blah’,E_USER_ ERROR); }
  • 16. Suppressing ErrorsSuppressing Errors $db = @mysql_connect($h,$u,$p); if (!$db) { trigger_error(blah.',E_USER_ERROR ); } $db = @mysql_connect($h,$u,$p); Attempt to connect to database. Suppress error notice if it fails..
  • 17. Suppressing ErrorsSuppressing Errors $db = @mysql_connect($h,$u,$p); if (!$db) { trigger_error(‘blah’,E_USER_ERROR); } Since error is suppressed, it must be handled gracefully somewhere else..
  • 18. Suppressing ErrorsSuppressing Errors • Error suppression is NOT a solution to a problem. • It can be useful to locally define your own error handling mechanisms. • If you suppress any errors, you must check for them yourself elsewhere.
  • 19. Custom Error HandlerCustom Error Handler • You can write your own function to handle PHP errors in any way you want. • You simply need to write a function with appropriate inputs, then register it in your script as the error handler. • The handler function should be able to receive 4 arguments, and return true to indicate it has handled the error…
  • 20. Custom Error HandlerCustom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; return true; }
  • 21. Custom Error HandlerCustom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; return true; } $errcode,$errmsg,$file,$lineno) { The handler must have 4 inputs.. 1.error code 2.error message 3.file where error occurred 4.line at which error occurred
  • 22. Custom Error HandlerCustom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; return true; } echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; Any PHP statements can be executed…
  • 23. Custom Error HandlerCustom Error Handler function err_handler( $errcode,$errmsg,$file,$lineno) { echo ‘An error has occurred!<br />’; echo “file: $file<br />”; echo “line: $lineno<br />”; echo “Problem: $errmsg”; return true; } return true; Return true to let PHP know that the custom error handler has handled the error OK.
  • 24. Custom Error HandlerCustom Error Handler • The function then needs to be registered as your custom error handler: set_error_handler(‘err_handler’); • You can ‘mask’ the custom error handler so it only receives certain types of error. e.g. to register a custom handler just for user triggered errors: set_error_handler(‘err_handler’, E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR);
  • 25. Custom Error HandlerCustom Error Handler • A custom error handler is never passed E_PARSE, E_CORE_ERROR or E_COMPILE_ERROR errors as these are considered too dangerous. • Often used in conjunction with a ‘debug’ flag for neat combination of debug and production code display..
  • 26. ThankThank You !!!You !!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/php-classes-in-mumbai.html

Hinweis der Redaktion

  1. Outline how you can cause errors yourself..