SlideShare a Scribd company logo
Handling Error & Exception in PHP
Presenter: Pravasini Sahoo, Mindfire Solutions
Date: 16/07/2015 1
Presenter: Pravasini Sahoo, Mindfire Solutions
Working as a PHP developer in MFS for more than 5 years.
My skill is on PHP, MySQL, HTML, HTML5, CSS, CSS3, jQuery, AJAX, Javascript, XML, ZendFramework, Goog
Zend Certified Engineer (ZCE) - Zend PHP 5.3 Certification
Oracle Certified Professional - OCP MySQL 5 Developer - Part 1 - 1Z0-871
Microsoft Certified Professional - MCP - Programming in HTML5 with Javascript & CSS3
Best Web Dev Article for month of January in CodeProject
Speaker in GDG group (Srusti Academy of Management, Koustuv Group)
Speaker in MSDC group (USBM)
Speaker in GDG group (IIT Bhubaneswar on HTML5 & CSS3)
2
Presenter: Pravasini Sahoo, Mindfire Solutions
What are Errors
Types of PHP Errors
Ways to handle PHP Errors
What are Exceptions
Handling Exceptions
Default Exception handler
Conclusion
Questions!!!
3
Presenter: Pravasini Sahoo, Mindfire Solutions
4
Presenter: Pravasini Sahoo, Mindfire Solutions
An Error is a term used to describe any issue that arises
unexpectedly that cause a computer to not function
properly.
5
Presenter: Pravasini Sahoo, Mindfire Solutions
6
Presenter: Pravasini Sahoo, Mindfire Solutions
7
These are errors in which the code behaves unexpectedly due to
some code in the program not acting as expected.
Can use newrelic for finding and fixing problems in the server -
https://docs.newrelic.com/docs/agents/php-
agent/installation/php-agent-installation-ubuntu-debian
Sentry : It is very useful for logging the errors/exceptions
without writing the code manually. It will group the same error
messages and will be logged in a nice manner -
https://www.getsentry.com/welcome/
Presenter: Pravasini Sahoo, Mindfire Solutions
E_ERROR
E_WARNING
E_NOTICE
E_USER_ERROR
E_USER_WARNING
E_USER_NOTICE
E_RECOVERABLE_ERROR
E_ALL
8
Presenter: Pravasini Sahoo, Mindfire Solutions
In general, these kind of errors can not be recoverable and it halts the
execution of the running script.
undefinedFunction();
#Fatal error: Call to undefined function undefinedFunction() in /test-
project/test.php on line 1
9
Presenter: Pravasini Sahoo, Mindfire Solutions
Generally, these errors are non-fatal runtime errors and can be
recoverable. It does not halt the execution and also it indicates the
system/user that some problem has happened.
include(“missing-file.php”);
#Warning: include(file.php): failed to open stream: No such file or directory
in /test-project/test.php on line 1
#Warning: include(): Failed opening 'file.php' for inclusion
(include_path='.:') in /test-project/test.php on line 1
10
Presenter: Pravasini Sahoo, Mindfire Solutions
These type of error produces notices in run-time context. Generally, it
happens when the script found something wrong which can show error while
running the script.
$a = $b + 1;
#Notice: Undefined variable: b in /test-project/test.php on line 1
11
Presenter: Pravasini Sahoo, Mindfire Solutions
These are like E_ERROR but set by the user programmatically using
trigger_error().
if (function_exists(undefinedFunction)) {
undefinedFunction();
} else {
trigger_error('The function you called, does not exists',
E_USER_ERROR);
}
#Notice: Use of undefined constant undefinedFunction - assumed
'undefinedFunction' in /test-project/test.php on line 1
#Fatal error: The function you called, does not exists in /test-
project/test.php on line 4
12
Presenter: Pravasini Sahoo, Mindfire Solutions
These are like E_WARNING but set by the user programmatically using
trigger_error().
if (include("file.php")) {
include("file.php");
} else {
trigger_error('The file you are searching for not found',
E_USER_WARNING);
}
#Warning: include(file.php): failed to open stream: No such file or directory
in /test-project/test.php on line 1
#Warning: include(): Failed opening 'file.php' for inclusion
(include_path='.:') in /test-project/test.php on line 1
#Warning: The file you are searching for not found in /test-project/test.php
on line 4
13
Presenter: Pravasini Sahoo, Mindfire Solutions
These are like E_NOTICE but set by the user programmatically using
trigger_error().
if (isset($b)) {
$a = $b + 1;
} else {
trigger_error('$b is not defined yet');
}
#Notice: $b is not defined yet in /test-project/test.php on line 4
14
Presenter: Pravasini Sahoo, Mindfire Solutions
15
Presenter: Pravasini Sahoo, Mindfire Solutions
Show the errors to users
Log the errors
Ignore them, as like nothing has happened
Act on them and fix it
16
Presenter: Pravasini Sahoo, Mindfire Solutions
display_errors = on/1 in php.ini file
ini_set(‘display_errors’,1); in application config file
php_value display_errors 1 in .htaccess file
Set error_reporting() to the appropriate severity level
17
Presenter: Pravasini Sahoo, Mindfire Solutions
log_errors = On in php.ini file
error_log = path/to/file in php.ini file
error_log(‘User Defined Error’); in your application
Send the errors through email
Log the errors in DB
Log into physical file structure
18
Presenter: Pravasini Sahoo, Mindfire Solutions
Suppress Errors using @ operator
Like @fopen or @file_get_contents
19
Presenter: Pravasini Sahoo, Mindfire Solutions
Using set_error_handler(<user_defined_error_handler>)
It has 5 parameters, which is used to get proper error
message
error_level
error_message
error_file
error_line
error_context
20
Presenter: Pravasini Sahoo, Mindfire Solutions
21
Presenter: Pravasini Sahoo, Mindfire Solutions
In general, exception is a certain thing which is excluded from a general
statement or does not follow the rule.
In computer language, Exception handling is the process of responding to
the occurrence, during computation, often changing the normal flow of
program execution.
22
Presenter: Pravasini Sahoo, Mindfire Solutions
23
Presenter: Pravasini Sahoo, Mindfire Solutions
try —> Try to run the code which may throw exception
throw —> Triggering an exception
catch —> Handle the thrown exception
finally —> Default code block to run, even after exception
Not available in PHP
24
Presenter: Pravasini Sahoo, Mindfire Solutions
25
Presenter: Pravasini Sahoo, Mindfire Solutions
user-defined Exception Handler
It handles any uncaught exception
After handling, the script halts
function defaultExceptionHandler() {
// code to handle exception
}
set_exception_handler(‘defaultExceptionHandler’);
class CustomExceptionHandler() {
public function handleException() {
// code to handle exception
}
}
set_exception handler(array(‘CustomExceptionHandler’,
‘handleException’));
26
Presenter: Pravasini Sahoo, Mindfire Solutions
27
Presenter: Pravasini Sahoo, Mindfire Solutions
Error or Exceptions are usual in PHP
Logging is very important
Do not ignore the errors
If not handled properly, they will turn into bugs
Always have a default error handler to log the errors and
handle in an efficient way, even if we know our code will
never show any error :)
28
Presenter: Pravasini Sahoo, Mindfire Solutions
29
Presenter: Pravasini Sahoo, Mindfire Solutions
http://www.computerhope.com/jargon/e/error.htm
http://www.w3schools.com/php/php_exception.asp
http://www.informit.com/articles/article.aspx
30
Presenter: Pravasini Sahoo, Mindfire Solutions
31
https://www.facebook.com/pravasini.sahoo.9
http://in.linkedin.com/pub/pravasini-sahoo/22/a4a/80
https://twitter.com/pravasinisahoo
Presenter: Pravasini Sahoo, Mindfire SolutionsPresenter: Pravasini Sahoo, Mindfire Solutions
https://plus.google.com/116915409923931471434/
32

More Related Content

PDF
Php exceptions
PDF
Introduction to php exception and error management
PPT
PHP - Introduction to PHP Error Handling
PPT
Php Error Handling
PPT
Error reporting in php
PDF
Errors, Exceptions & Logging (PHP Hants Oct '13)
PDF
Elegant Ways of Handling PHP Errors and Exceptions
PDF
Types of Error in PHP
Php exceptions
Introduction to php exception and error management
PHP - Introduction to PHP Error Handling
Php Error Handling
Error reporting in php
Errors, Exceptions & Logging (PHP Hants Oct '13)
Elegant Ways of Handling PHP Errors and Exceptions
Types of Error in PHP

What's hot (20)

PDF
Exceptions in PHP
PPT
PHP - Introduction to PHP - Mazenet Solution
PPTX
Exception handling in ASP .NET
ODP
Perl exceptions lightning talk
PPT
Php(report)
PDF
The PHP Way Of TDD - Think First, Code Later
PPT
Bioinformatica 27-10-2011-p4-files
PPTX
Exception handling in asp.net
PPTX
Python Programming Essentials - M21 - Exception Handling
PPT
Exception handling
PPTX
Presentation1
PDF
Introduction to PHP - Basics of PHP
PPTX
Basic of PHP
PDF
Methods of debugging - Atomate.net
PPT
Introduction to PHP
PPTX
PHP Basics
ODP
Debugging With Php
PPTX
Java programming-Event Handling
PPT
Phpunit
PDF
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exceptions in PHP
PHP - Introduction to PHP - Mazenet Solution
Exception handling in ASP .NET
Perl exceptions lightning talk
Php(report)
The PHP Way Of TDD - Think First, Code Later
Bioinformatica 27-10-2011-p4-files
Exception handling in asp.net
Python Programming Essentials - M21 - Exception Handling
Exception handling
Presentation1
Introduction to PHP - Basics of PHP
Basic of PHP
Methods of debugging - Atomate.net
Introduction to PHP
PHP Basics
Debugging With Php
Java programming-Event Handling
Phpunit
Exception Handling in Python - Rik van Achterberg & Tim Muller
Ad

Viewers also liked (18)

PPTX
Chapter v(error)
PPT
Evolution of java
PPTX
JDBC Driver Types
PPSX
Exception Handling
PPTX
Chapter 5-MATH ITU GUYS
DOCX
الماء1
PPTX
сonnect and protect
PDF
Bounds on double domination in squares of graphs
PDF
Deber de informatica
PDF
Our work Kovit Thamrong School & Peter House : Niwat Architects Chiangmai
PPT
java concepts
PDF
Oscillatory motion control of hinged body using controller
PDF
Cahit 8-equitability of coronas cn○k1
PPT
B1 l1 3 powerpoint
PDF
Khawaja Muhammad Safdar Medical College Sialkot Merit List 2014
PPTX
InvestmentNews 2015 Adviser Technology Study: Key Findings
PPTX
Foursquare
Chapter v(error)
Evolution of java
JDBC Driver Types
Exception Handling
Chapter 5-MATH ITU GUYS
الماء1
сonnect and protect
Bounds on double domination in squares of graphs
Deber de informatica
Our work Kovit Thamrong School & Peter House : Niwat Architects Chiangmai
java concepts
Oscillatory motion control of hinged body using controller
Cahit 8-equitability of coronas cn○k1
B1 l1 3 powerpoint
Khawaja Muhammad Safdar Medical College Sialkot Merit List 2014
InvestmentNews 2015 Adviser Technology Study: Key Findings
Foursquare
Ad

Similar to Handling error & exception in php (20)

PPT
PHP Interview Questions-ppt
PDF
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
PDF
Cursus phpunit
PPT
Php mysql training-in-mumbai
PPTX
PHPUnit with CakePHP and Yii
DOC
Error Handling in Compiler Design.doctyp
DOC
Error Handling in Compiler Design.What a
DOC
Error Handling in Compiler Design.typeso
PPTX
debuggingSession.pptx
PPTX
Error handling
PPTX
Error and Exception Handling in PHP
PDF
Behavior & Specification Driven Development in PHP - #OpenWest
PPT
Phpunit testing
PPT
Php security
PPTX
It Works On Dev
PDF
Session10-PHP Misconfiguration
PPT
Zend Framework 2 - PHPUnit
PHP Interview Questions-ppt
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
Cursus phpunit
Php mysql training-in-mumbai
PHPUnit with CakePHP and Yii
Error Handling in Compiler Design.doctyp
Error Handling in Compiler Design.What a
Error Handling in Compiler Design.typeso
debuggingSession.pptx
Error handling
Error and Exception Handling in PHP
Behavior & Specification Driven Development in PHP - #OpenWest
Phpunit testing
Php security
It Works On Dev
Session10-PHP Misconfiguration
Zend Framework 2 - PHPUnit

Recently uploaded (20)

PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
ai tools demonstartion for schools and inter college
PPTX
history of c programming in notes for students .pptx
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
L1 - Introduction to python Backend.pptx
PDF
Designing Intelligence for the Shop Floor.pdf
Odoo Companies in India – Driving Business Transformation.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Choose the Right IT Partner for Your Business in Malaysia
Wondershare Filmora 15 Crack With Activation Key [2025
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
ai tools demonstartion for schools and inter college
history of c programming in notes for students .pptx
Digital Systems & Binary Numbers (comprehensive )
VVF-Customer-Presentation2025-Ver1.9.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
wealthsignaloriginal-com-DS-text-... (1).pdf
Computer Software and OS of computer science of grade 11.pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Softaken Excel to vCard Converter Software.pdf
Nekopoi APK 2025 free lastest update
PTS Company Brochure 2025 (1).pdf.......
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
L1 - Introduction to python Backend.pptx
Designing Intelligence for the Shop Floor.pdf

Handling error & exception in php

  • 1. Handling Error & Exception in PHP Presenter: Pravasini Sahoo, Mindfire Solutions Date: 16/07/2015 1
  • 2. Presenter: Pravasini Sahoo, Mindfire Solutions Working as a PHP developer in MFS for more than 5 years. My skill is on PHP, MySQL, HTML, HTML5, CSS, CSS3, jQuery, AJAX, Javascript, XML, ZendFramework, Goog Zend Certified Engineer (ZCE) - Zend PHP 5.3 Certification Oracle Certified Professional - OCP MySQL 5 Developer - Part 1 - 1Z0-871 Microsoft Certified Professional - MCP - Programming in HTML5 with Javascript & CSS3 Best Web Dev Article for month of January in CodeProject Speaker in GDG group (Srusti Academy of Management, Koustuv Group) Speaker in MSDC group (USBM) Speaker in GDG group (IIT Bhubaneswar on HTML5 & CSS3) 2
  • 3. Presenter: Pravasini Sahoo, Mindfire Solutions What are Errors Types of PHP Errors Ways to handle PHP Errors What are Exceptions Handling Exceptions Default Exception handler Conclusion Questions!!! 3
  • 4. Presenter: Pravasini Sahoo, Mindfire Solutions 4
  • 5. Presenter: Pravasini Sahoo, Mindfire Solutions An Error is a term used to describe any issue that arises unexpectedly that cause a computer to not function properly. 5
  • 6. Presenter: Pravasini Sahoo, Mindfire Solutions 6
  • 7. Presenter: Pravasini Sahoo, Mindfire Solutions 7 These are errors in which the code behaves unexpectedly due to some code in the program not acting as expected. Can use newrelic for finding and fixing problems in the server - https://docs.newrelic.com/docs/agents/php- agent/installation/php-agent-installation-ubuntu-debian Sentry : It is very useful for logging the errors/exceptions without writing the code manually. It will group the same error messages and will be logged in a nice manner - https://www.getsentry.com/welcome/
  • 8. Presenter: Pravasini Sahoo, Mindfire Solutions E_ERROR E_WARNING E_NOTICE E_USER_ERROR E_USER_WARNING E_USER_NOTICE E_RECOVERABLE_ERROR E_ALL 8
  • 9. Presenter: Pravasini Sahoo, Mindfire Solutions In general, these kind of errors can not be recoverable and it halts the execution of the running script. undefinedFunction(); #Fatal error: Call to undefined function undefinedFunction() in /test- project/test.php on line 1 9
  • 10. Presenter: Pravasini Sahoo, Mindfire Solutions Generally, these errors are non-fatal runtime errors and can be recoverable. It does not halt the execution and also it indicates the system/user that some problem has happened. include(“missing-file.php”); #Warning: include(file.php): failed to open stream: No such file or directory in /test-project/test.php on line 1 #Warning: include(): Failed opening 'file.php' for inclusion (include_path='.:') in /test-project/test.php on line 1 10
  • 11. Presenter: Pravasini Sahoo, Mindfire Solutions These type of error produces notices in run-time context. Generally, it happens when the script found something wrong which can show error while running the script. $a = $b + 1; #Notice: Undefined variable: b in /test-project/test.php on line 1 11
  • 12. Presenter: Pravasini Sahoo, Mindfire Solutions These are like E_ERROR but set by the user programmatically using trigger_error(). if (function_exists(undefinedFunction)) { undefinedFunction(); } else { trigger_error('The function you called, does not exists', E_USER_ERROR); } #Notice: Use of undefined constant undefinedFunction - assumed 'undefinedFunction' in /test-project/test.php on line 1 #Fatal error: The function you called, does not exists in /test- project/test.php on line 4 12
  • 13. Presenter: Pravasini Sahoo, Mindfire Solutions These are like E_WARNING but set by the user programmatically using trigger_error(). if (include("file.php")) { include("file.php"); } else { trigger_error('The file you are searching for not found', E_USER_WARNING); } #Warning: include(file.php): failed to open stream: No such file or directory in /test-project/test.php on line 1 #Warning: include(): Failed opening 'file.php' for inclusion (include_path='.:') in /test-project/test.php on line 1 #Warning: The file you are searching for not found in /test-project/test.php on line 4 13
  • 14. Presenter: Pravasini Sahoo, Mindfire Solutions These are like E_NOTICE but set by the user programmatically using trigger_error(). if (isset($b)) { $a = $b + 1; } else { trigger_error('$b is not defined yet'); } #Notice: $b is not defined yet in /test-project/test.php on line 4 14
  • 15. Presenter: Pravasini Sahoo, Mindfire Solutions 15
  • 16. Presenter: Pravasini Sahoo, Mindfire Solutions Show the errors to users Log the errors Ignore them, as like nothing has happened Act on them and fix it 16
  • 17. Presenter: Pravasini Sahoo, Mindfire Solutions display_errors = on/1 in php.ini file ini_set(‘display_errors’,1); in application config file php_value display_errors 1 in .htaccess file Set error_reporting() to the appropriate severity level 17
  • 18. Presenter: Pravasini Sahoo, Mindfire Solutions log_errors = On in php.ini file error_log = path/to/file in php.ini file error_log(‘User Defined Error’); in your application Send the errors through email Log the errors in DB Log into physical file structure 18
  • 19. Presenter: Pravasini Sahoo, Mindfire Solutions Suppress Errors using @ operator Like @fopen or @file_get_contents 19
  • 20. Presenter: Pravasini Sahoo, Mindfire Solutions Using set_error_handler(<user_defined_error_handler>) It has 5 parameters, which is used to get proper error message error_level error_message error_file error_line error_context 20
  • 21. Presenter: Pravasini Sahoo, Mindfire Solutions 21
  • 22. Presenter: Pravasini Sahoo, Mindfire Solutions In general, exception is a certain thing which is excluded from a general statement or does not follow the rule. In computer language, Exception handling is the process of responding to the occurrence, during computation, often changing the normal flow of program execution. 22
  • 23. Presenter: Pravasini Sahoo, Mindfire Solutions 23
  • 24. Presenter: Pravasini Sahoo, Mindfire Solutions try —> Try to run the code which may throw exception throw —> Triggering an exception catch —> Handle the thrown exception finally —> Default code block to run, even after exception Not available in PHP 24
  • 25. Presenter: Pravasini Sahoo, Mindfire Solutions 25
  • 26. Presenter: Pravasini Sahoo, Mindfire Solutions user-defined Exception Handler It handles any uncaught exception After handling, the script halts function defaultExceptionHandler() { // code to handle exception } set_exception_handler(‘defaultExceptionHandler’); class CustomExceptionHandler() { public function handleException() { // code to handle exception } } set_exception handler(array(‘CustomExceptionHandler’, ‘handleException’)); 26
  • 27. Presenter: Pravasini Sahoo, Mindfire Solutions 27
  • 28. Presenter: Pravasini Sahoo, Mindfire Solutions Error or Exceptions are usual in PHP Logging is very important Do not ignore the errors If not handled properly, they will turn into bugs Always have a default error handler to log the errors and handle in an efficient way, even if we know our code will never show any error :) 28
  • 29. Presenter: Pravasini Sahoo, Mindfire Solutions 29
  • 30. Presenter: Pravasini Sahoo, Mindfire Solutions http://www.computerhope.com/jargon/e/error.htm http://www.w3schools.com/php/php_exception.asp http://www.informit.com/articles/article.aspx 30
  • 31. Presenter: Pravasini Sahoo, Mindfire Solutions 31