SlideShare a Scribd company logo
PHP -  Introduction to PHP Error Handling
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
Ad

Recommended

Php Error Handling
Php Error Handling
mussawir20
Ā 
Form using html and java script validation
Form using html and java script validation
Maitree Patel
Ā 
React-JS Component Life-cycle Methods
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
Ā 
Procedures functions structures in VB.Net
Procedures functions structures in VB.Net
tjunicornfx
Ā 
XML Document Object Model (DOM)
XML Document Object Model (DOM)
BOSS Webtech
Ā 
Form Validation in JavaScript
Form Validation in JavaScript
Ravi Bhadauria
Ā 
JavaScript Promises
JavaScript Promises
L&T Technology Services Limited
Ā 
Form validation client side
Form validation client side
Mudasir Syed
Ā 
jQuery - Chapter 3 - Effects
jQuery - Chapter 3 - Effects
WebStackAcademy
Ā 
Web container and Apache Tomcat
Web container and Apache Tomcat
Auwal Amshi
Ā 
JQuery introduction
JQuery introduction
NexThoughts Technologies
Ā 
Java script
Java script
Shyam Khant
Ā 
jQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
Ā 
jQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
Ā 
android sqlite
android sqlite
Deepa Rani
Ā 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
Adnan Sohail
Ā 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
Ā 
Div tag presentation
Div tag presentation
alyssa_lum11
Ā 
JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
Ā 
Vectors in Java
Vectors in Java
Abhilash Nair
Ā 
Master page in Asp.net
Master page in Asp.net
RupinderjitKaur9
Ā 
Javascript functions
Javascript functions
Alaref Abushaala
Ā 
Functional programming
Functional programming
ijcd
Ā 
jQuery PPT
jQuery PPT
Dominic Arrojado
Ā 
Window object
Window object
preetikapri1
Ā 
ASP.NET Page Life Cycle
ASP.NET Page Life Cycle
Abhishek Sur
Ā 
Android adapters
Android adapters
baabtra.com - No. 1 supplier of quality freshers
Ā 
Beginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
Ā 
Exceptions in PHP
Exceptions in PHP
JanTvrdik
Ā 
Php exceptions
Php exceptions
Damian Sromek
Ā 

More Related Content

What's hot (20)

jQuery - Chapter 3 - Effects
jQuery - Chapter 3 - Effects
WebStackAcademy
Ā 
Web container and Apache Tomcat
Web container and Apache Tomcat
Auwal Amshi
Ā 
JQuery introduction
JQuery introduction
NexThoughts Technologies
Ā 
Java script
Java script
Shyam Khant
Ā 
jQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
Ā 
jQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
Ā 
android sqlite
android sqlite
Deepa Rani
Ā 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
Adnan Sohail
Ā 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
Ā 
Div tag presentation
Div tag presentation
alyssa_lum11
Ā 
JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
Ā 
Vectors in Java
Vectors in Java
Abhilash Nair
Ā 
Master page in Asp.net
Master page in Asp.net
RupinderjitKaur9
Ā 
Javascript functions
Javascript functions
Alaref Abushaala
Ā 
Functional programming
Functional programming
ijcd
Ā 
jQuery PPT
jQuery PPT
Dominic Arrojado
Ā 
Window object
Window object
preetikapri1
Ā 
ASP.NET Page Life Cycle
ASP.NET Page Life Cycle
Abhishek Sur
Ā 
Android adapters
Android adapters
baabtra.com - No. 1 supplier of quality freshers
Ā 
Beginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
Ā 
jQuery - Chapter 3 - Effects
jQuery - Chapter 3 - Effects
WebStackAcademy
Ā 
Web container and Apache Tomcat
Web container and Apache Tomcat
Auwal Amshi
Ā 
Java script
Java script
Shyam Khant
Ā 
jQuery from the very beginning
jQuery from the very beginning
Anis Ahmad
Ā 
android sqlite
android sqlite
Deepa Rani
Ā 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
Adnan Sohail
Ā 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
Ā 
Div tag presentation
Div tag presentation
alyssa_lum11
Ā 
JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
Ā 
Vectors in Java
Vectors in Java
Abhilash Nair
Ā 
Master page in Asp.net
Master page in Asp.net
RupinderjitKaur9
Ā 
Javascript functions
Javascript functions
Alaref Abushaala
Ā 
Functional programming
Functional programming
ijcd
Ā 
Window object
Window object
preetikapri1
Ā 
ASP.NET Page Life Cycle
ASP.NET Page Life Cycle
Abhishek Sur
Ā 
Beginners PHP Tutorial
Beginners PHP Tutorial
alexjones89
Ā 

Viewers also liked (6)

Exceptions in PHP
Exceptions in PHP
JanTvrdik
Ā 
Php exceptions
Php exceptions
Damian Sromek
Ā 
Errors, Exceptions & Logging (PHP Hants Oct '13)
Errors, Exceptions & Logging (PHP Hants Oct '13)
James Titcumb
Ā 
Elegant Ways of Handling PHP Errors and Exceptions
Elegant Ways of Handling PHP Errors and Exceptions
ZendCon
Ā 
Use of Monolog with PHP
Use of Monolog with PHP
Mindfire Solutions
Ā 
Handling error & exception in php
Handling error & exception in php
Pravasini Sahoo
Ā 
Exceptions in PHP
Exceptions in PHP
JanTvrdik
Ā 
Php exceptions
Php exceptions
Damian Sromek
Ā 
Errors, Exceptions & Logging (PHP Hants Oct '13)
Errors, Exceptions & Logging (PHP Hants Oct '13)
James Titcumb
Ā 
Elegant Ways of Handling PHP Errors and Exceptions
Elegant Ways of Handling PHP Errors and Exceptions
ZendCon
Ā 
Handling error & exception in php
Handling error & exception in php
Pravasini Sahoo
Ā 
Ad

Similar to PHP - Introduction to PHP Error Handling (20)

Error Handling In PHP with all Try catch anf various runtime errors
Error Handling In PHP with all Try catch anf various runtime errors
PraveenHegde20
Ā 
Error reporting in php
Error reporting in php
Mudasir Syed
Ā 
lecture 15.pptx
lecture 15.pptx
ITNet
Ā 
Introduction to php exception and error management
Introduction to php exception and error management
baabtra.com - No. 1 supplier of quality freshers
Ā 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
Jalpesh Vasa
Ā 
Error handling
Error handling
Meherul1234
Ā 
Getting modern with logging via log4perl
Getting modern with logging via log4perl
Dean Hamstead
Ā 
Sending emails through PHP
Sending emails through PHP
krishnapriya Tadepalli
Ā 
TDC 2015 - POA - Trilha PHP - Shit Happens
TDC 2015 - POA - Trilha PHP - Shit Happens
Jackson F. de A. Mafra
Ā 
Error and Exception Handling in PHP
Error and Exception Handling in PHP
Arafat Hossan
Ā 
Error handling and debugging
Error handling and debugging
salissal
Ā 
Session10-PHP Misconfiguration
Session10-PHP Misconfiguration
zakieh alizadeh
Ā 
PerlScripting
PerlScripting
Aureliano Bombarely
Ā 
Module-4_WTA_PHP Class & Error Handling
Module-4_WTA_PHP Class & Error Handling
SIVAKUMAR V
Ā 
Php
Php
ksujitha
Ā 
Error handling in XPages
Error handling in XPages
dominion
Ā 
ELMAH
ELMAH
Mindfire Solutions
Ā 
Debugging With Php
Debugging With Php
Automatem Ltd
Ā 
Debugging
Debugging
nicky_walters
Ā 
Errors
Errors
agjmills
Ā 
Error Handling In PHP with all Try catch anf various runtime errors
Error Handling In PHP with all Try catch anf various runtime errors
PraveenHegde20
Ā 
Error reporting in php
Error reporting in php
Mudasir Syed
Ā 
lecture 15.pptx
lecture 15.pptx
ITNet
Ā 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
Jalpesh Vasa
Ā 
Error handling
Error handling
Meherul1234
Ā 
Getting modern with logging via log4perl
Getting modern with logging via log4perl
Dean Hamstead
Ā 
TDC 2015 - POA - Trilha PHP - Shit Happens
TDC 2015 - POA - Trilha PHP - Shit Happens
Jackson F. de A. Mafra
Ā 
Error and Exception Handling in PHP
Error and Exception Handling in PHP
Arafat Hossan
Ā 
Error handling and debugging
Error handling and debugging
salissal
Ā 
Session10-PHP Misconfiguration
Session10-PHP Misconfiguration
zakieh alizadeh
Ā 
Module-4_WTA_PHP Class & Error Handling
Module-4_WTA_PHP Class & Error Handling
SIVAKUMAR V
Ā 
Error handling in XPages
Error handling in XPages
dominion
Ā 
Debugging With Php
Debugging With Php
Automatem Ltd
Ā 
Errors
Errors
agjmills
Ā 
Ad

More from Vibrant Technologies & Computers (20)

Buisness analyst business analysis overview ppt 5
Buisness analyst business analysis overview ppt 5
Vibrant Technologies & Computers
Ā 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
Ā 
SQL- Introduction to MySQL
SQL- Introduction to MySQL
Vibrant Technologies & Computers
Ā 
SQL- Introduction to SQL database
SQL- Introduction to SQL database
Vibrant Technologies & Computers
Ā 
ITIL - introduction to ITIL
ITIL - introduction to ITIL
Vibrant Technologies & Computers
Ā 
Salesforce - Introduction to Security & Access
Salesforce - Introduction to Security & Access
Vibrant Technologies & Computers
Ā 
Data ware housing- Introduction to olap .
Data ware housing- Introduction to olap .
Vibrant Technologies & Computers
Ā 
Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.
Vibrant Technologies & Computers
Ā 
Data ware housing- Introduction to data ware housing
Data ware housing- Introduction to data ware housing
Vibrant Technologies & Computers
Ā 
Salesforce - classification of cloud computing
Salesforce - classification of cloud computing
Vibrant Technologies & Computers
Ā 
Salesforce - cloud computing fundamental
Salesforce - cloud computing fundamental
Vibrant Technologies & Computers
Ā 
SQL- Introduction to PL/SQL
SQL- Introduction to PL/SQL
Vibrant Technologies & Computers
Ā 
SQL- Introduction to advanced sql concepts
SQL- Introduction to advanced sql concepts
Vibrant Technologies & Computers
Ā 
SQL Inteoduction to SQL manipulating of data
SQL Inteoduction to SQL manipulating of data
Vibrant Technologies & Computers
Ā 
SQL- Introduction to SQL Set Operations
SQL- Introduction to SQL Set Operations
Vibrant Technologies & Computers
Ā 
Sas - Introduction to designing the data mart
Sas - Introduction to designing the data mart
Vibrant Technologies & Computers
Ā 
Sas - Introduction to working under change management
Sas - Introduction to working under change management
Vibrant Technologies & Computers
Ā 
SAS - overview of SAS
SAS - overview of SAS
Vibrant Technologies & Computers
Ā 
Teradata - Architecture of Teradata
Teradata - Architecture of Teradata
Vibrant Technologies & Computers
Ā 
Teradata - Restoring Data
Teradata - Restoring Data
Vibrant Technologies & Computers
Ā 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
Vibrant Technologies & Computers
Ā 
Data ware housing - Introduction to data ware housing process.
Data ware housing - Introduction to data ware housing process.
Vibrant Technologies & Computers
Ā 
Data ware housing- Introduction to data ware housing
Data ware housing- Introduction to data ware housing
Vibrant Technologies & Computers
Ā 
Sas - Introduction to working under change management
Sas - Introduction to working under change management
Vibrant Technologies & Computers
Ā 

Recently uploaded (20)

Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
Ā 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
Ā 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
Ā 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
Ā 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
Ā 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
Ā 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
Ā 
ā€œMPU+: A Transformative Solution for Next-Gen AI at the Edge,ā€ a Presentation...
ā€œMPU+: A Transformative Solution for Next-Gen AI at the Edge,ā€ a Presentation...
Edge AI and Vision Alliance
Ā 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
Ā 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
Ā 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
Ā 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
Ā 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
Ā 
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
All Things Open
Ā 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
Ā 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
Ā 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
Ā 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
Ā 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
Ā 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
Ā 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
Ā 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
Ā 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
Ā 
Mastering AI Workflows with FME by Mark Döring
Mastering AI Workflows with FME by Mark Döring
Safe Software
Ā 
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
ReSTIR [DI]: Spatiotemporal reservoir resampling for real-time ray tracing ...
revolcs10
Ā 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
Ā 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
Ā 
ā€œMPU+: A Transformative Solution for Next-Gen AI at the Edge,ā€ a Presentation...
ā€œMPU+: A Transformative Solution for Next-Gen AI at the Edge,ā€ a Presentation...
Edge AI and Vision Alliance
Ā 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
Ā 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
Ā 
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
Ā 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
Ā 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
Ā 
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
All Things Open
Ā 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
Ā 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
Ā 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
Ā 
Python Conference Singapore - 19 Jun 2025
Python Conference Singapore - 19 Jun 2025
ninefyi
Ā 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
Ā 
The Future of Product Management in AI ERA.pdf
The Future of Product Management in AI ERA.pdf
Alyona Owens
Ā 

PHP - Introduction to PHP Error Handling

  • 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

Editor's Notes

  • #9: Outline how you can cause errors yourself..