PHP Arrays & Classes
Developed by
Mr.V.Sivakumar
PHP Arrays
• In general, an array is a data structure that allows the
programmer to collect a number of related elements
together in a single variable.
• Unlike most other programming languages, in PHP an
array is actually an ordered map, which associates each
value in the array with a key.
• PHP array is not only like other languages arrays, but it
is also like their vector, hash table, dictionary, and list
collections.
Multidimensional arrays
Multidimensional arrays
superglobal arrays
• PHP uses special predefined associative arrays called superglobal variables
that allow the programmer to easily access HTTP headers, query string
parameters.
• They are called superglobal because these arrays are always in scope and
always exist, ready for the programmer to access or modify them without
having to use the global keyword.
Errors and Exceptions
• A failure in any one of these systems will mean that the web application
will no longer run successfully.
• Types of errors
■ Expected errors
■ Warnings
■ Fatal errors
• An expected error is an error that routinely occurs during an application.
An error as a result of user inputs, for instance, entering letters when
numbers were expected.
• Users will leave fields blank, enter text when numbers were expected, type
in too much or too little text, forget to click certain things.
Errors and Exceptions
• Warnings, that generate a PHP warning message (which may or may
not be displayed) but will not halt the execution of the page. For
instance, calling a function without a required parameter will
generate a warning but not stop execution.
• Fatal errors, which are serious in that the execution of the page will
terminate unless handled in some way. These should truly be
exceptional and unexpected, such as a required input file being
missing or a database table or field disappearing.
PHP Error reporting
• There are three main error reporting flags:
■ error_reporting
■ display_errors
■ log_errors
• The error_reporting setting specifies which type of errors are to be
reported
error_reporting = E_ALL / E_ERROR / E_WARNING
• display_error setting specifies whether error messages should or
should not be displayed in the browser.
php.ini file: display_errors = Off
• log_error setting specifies whether error messages should or should
not be sent to the server error log log_errors = On
PHP Error and Exception handling
• Two mechanisms for handling runtime errors:
■ procedural error handling
■ Object-Oriented exception handling
• procedural approach to error handling, the programmer needs to
explicitly test for error conditions after performing a task that might
generate an error.
If(condition){ some code here } else { }
• 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 using
try{ condition} catch(Exception e) {}
PHP Error and Exception handling
• Custom error and exception handlers
• 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 user with a generic message such as “Sorry but there was
a problem,” or “Sorry but the system is down for
maintenance.”
Function myException($Exception)
{ Our own exception message / Code here }
Source Reference: Randy Connolly, Ricardo Hoar, Fundamentals of Web Development, Pearson
Thank you

Module-4_WTA_PHP Class & Error Handling

  • 1.
    PHP Arrays &Classes Developed by Mr.V.Sivakumar
  • 2.
    PHP Arrays • Ingeneral, an array is a data structure that allows the programmer to collect a number of related elements together in a single variable. • Unlike most other programming languages, in PHP an array is actually an ordered map, which associates each value in the array with a key. • PHP array is not only like other languages arrays, but it is also like their vector, hash table, dictionary, and list collections.
  • 5.
  • 6.
  • 7.
    superglobal arrays • PHPuses special predefined associative arrays called superglobal variables that allow the programmer to easily access HTTP headers, query string parameters. • They are called superglobal because these arrays are always in scope and always exist, ready for the programmer to access or modify them without having to use the global keyword.
  • 9.
    Errors and Exceptions •A failure in any one of these systems will mean that the web application will no longer run successfully. • Types of errors ■ Expected errors ■ Warnings ■ Fatal errors • An expected error is an error that routinely occurs during an application. An error as a result of user inputs, for instance, entering letters when numbers were expected. • Users will leave fields blank, enter text when numbers were expected, type in too much or too little text, forget to click certain things.
  • 10.
    Errors and Exceptions •Warnings, that generate a PHP warning message (which may or may not be displayed) but will not halt the execution of the page. For instance, calling a function without a required parameter will generate a warning but not stop execution. • Fatal errors, which are serious in that the execution of the page will terminate unless handled in some way. These should truly be exceptional and unexpected, such as a required input file being missing or a database table or field disappearing.
  • 11.
    PHP Error reporting •There are three main error reporting flags: ■ error_reporting ■ display_errors ■ log_errors • The error_reporting setting specifies which type of errors are to be reported error_reporting = E_ALL / E_ERROR / E_WARNING • display_error setting specifies whether error messages should or should not be displayed in the browser. php.ini file: display_errors = Off • log_error setting specifies whether error messages should or should not be sent to the server error log log_errors = On
  • 12.
    PHP Error andException handling • Two mechanisms for handling runtime errors: ■ procedural error handling ■ Object-Oriented exception handling • procedural approach to error handling, the programmer needs to explicitly test for error conditions after performing a task that might generate an error. If(condition){ some code here } else { } • 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 using try{ condition} catch(Exception e) {}
  • 13.
    PHP Error andException handling • Custom error and exception handlers • 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 user with a generic message such as “Sorry but there was a problem,” or “Sorry but the system is down for maintenance.” Function myException($Exception) { Our own exception message / Code here }
  • 14.
    Source Reference: RandyConnolly, Ricardo Hoar, Fundamentals of Web Development, Pearson Thank you