0% found this document useful (0 votes)
76 views24 pages

Mod 4-Chapter-12

..

Uploaded by

Shashwath Shenoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views24 pages

Mod 4-Chapter-12

..

Uploaded by

Shashwath Shenoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Error Handling and

Validation

Chapter 12

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Textbook to be published by Pearson ©
Ed2015
in early
Pearson
2014
Randy Connolly and Ricardo Hoar Fundamentals of http://www.funwebdev.com
Web Development
Objectives
1 What are Errors
and Exceptions? 2 PHP Error
Reporting

3 PHP Error and


Exception Handling 4 Regular
Expressions

5 Validating User
Input 6 Where to Perform
Validation

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Section 1 of 6
WHAT ARE ERRORS AND EXCEPTIONS?

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Types of Errors

• Expected errors
Things that you expect to go wrong. Bad user input,
database connection, etc…

• Warnings
problems that generate a PHP warning message
but will not halt the execution of the page

• Fatal errors
are serious in that the execution of the page will
terminate unless handled in some way

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Checking user input
Checking for values

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Checking user input
Checking for a number

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Exceptions vs Errors
Not the same thing

• An error is some type of problem that generates


a nonfatal warning message or that generates
an error message that terminates the program’s
execution.
• An exception refers to objects that are of type
Exception and which are used in conjunction
with the object-oriented try . . . catch language
construct for dealing with runtime errors.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


PHP ERROR REPORTING
Section 2 of 6

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


PHP error reporting
Lots of control

PHP has a flexible and customizable system for


reporting warnings and errors that can be set
programmatically at runtime or declaratively at
design-time within the php.ini file. There are three
main error reporting flags:
• error_reporting
• display_errors
• log_errors

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


The error_reporting setting
What is an error?

The error_reporting setting specifies which type of


errors are to be reported.
It can be set programmatically inside any PHP file:
error_reporting(E_ALL);

It can also be set within the php.ini file:


error_reporting = E_ALL

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


The error_reporting setting
Some error reporting constants

Constant Name Value Description

E_ALL 8191 Report all errors and warnings

E_ERROR 1 Report all fatal runtime errors

E_WARNING 2 Report all nonfatal runtime errors (that is, warnings)

0 No reporting

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


The display_errors setting
To show or not to show

The display_error setting specifies whether error


messages should or should not be displayed in the
browser.
It can be set programmatically via the ini_set()
function:
ini_set('display_errors','0');

It can also be set within the php.ini file:


display_errors = Off

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


The log_error setting
To record or not to record

The log_error setting specifies whether error


messages should or should not be sent to the
server error log.
It can be set programmatically via the ini_set()
function:
ini_set('log_errors','1');

It can also be set within the php.ini file:


log_errors = On

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


The log_error setting
Where to store.

The location to store logs in can be set


programatically:
ini_set('error_log', '/restricted/my-errors.log');

It can also be set within the php.ini file:


error_log = /restricted/my-errors.log

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


The log_error setting
Error_log()

You can also programmatically send messages to


the error log at any time via the error_log()
function

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Section 3 of 6
PHP ERROR AND EXCEPTION
HANDLING
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
Procedural Error Handling

Recall connecting to a database, that there may be


an error…

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


OO Exception Handling
Try, catch, finally

When a runtime error occurs, PHP throws an


exception.
This exception can be caught and handled either
by the function, class, or page that generated the
exception or by the code that called the function
or class.
If an exception is not caught, then eventually the
PHP environment will handle it by terminating
execution with an “Uncaught Exception” message.

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


OO Exception Handling
Try, catch, finally

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


OO Exception Handling
Finally

The finally block is optional. Any code within it will


always be executed after the code in the try or in
the catch blocks, even if that code contains a
return statement.
The finally block is only available in PHP 5.5 and
later

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Throw your own exception
Object oriented way of dealing with the unexpected

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Custom Handlers
Error and Exception Handlers

What should a custom error or exception handler do?


It should provide the developer with detailed
information about the state of the application when
the exception occurred, information about the
exception, and when it happened.
It should hide any of those details from the regular
end user, and instead provide the user with a generic
message such as “Sorry but there was a problem”
Once a handler function is defined, it must be
registered, using the following code:
set_exception_handler('my_exception_handler');

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


Custom Handlers
Error and Exception Handlers

Randy Connolly and Ricardo Hoar Fundamentals of Web Development


What You’ve Learned
1 What are Errors
and Exceptions? 2 PHP Error
Reporting

3 PHP Error and


Exception Handling 4 Regular
Expressions

5 Validating User
Input 6 Where to Perform
Validation

Randy Connolly and Ricardo Hoar Fundamentals of Web Development

You might also like