Now a days, PHP is the most popular server-side web programming language, so it’s important to know how to resolve any errors that may occur when PHP code is executed.
Here, we will look at the basics of how to do error handling in PHP, using the built-in functions, writing custom error handler functions, and with Exception error handling
There are two fundamental types of errors in software:
Once errors occur and we detect them, there are four fundamental ways to deal with them:
Here, we will focus on the displaying, logging, and handling of errors. Once we know how to handle errors, then we can act on them easily.
Depending on if we are running code in production or in development, we might want to turn on or off reporting certain types of errors.
There are different locations where we can control what error types are to be reported:
There are four possible options for showing PHP errors:
Let’s see how we can use these configuration options.
During development, we might want to turn all error reporting from the php.ini:
display_errors = on error_reporting = E_ALL
We could do the same from our PHP code during runtime by calling the error_reporting() function:
To turn on error logging and log the errors to our own specific log file (instead of the default error log file, which is often the web server error log file or the syslog file), we could configure PHP with:
log_errors = on error_log = "/tmp/php-error.log"
Now log errors via PHP code:
error_log( "This code has errors!" );
Now that we can configure PHP to tell it what types of errors to report on, we need to be able to handle different error types in our own way. Just displaying standard PHP error messages is often not good enough.
We want to handle the error and display our own messages when an error happens. For this, we can use the set_error_handler() function.
Here is a very simple custom error handling function:
// error handler function function errorHandler($errno, $errstr) { echo "Error: [$errno] $errstr"; } // set error handler set_error_handler("errorHandler"); // trigger an error echo($foo); // undefined variable!
So when an error occurs, the custom error handler function is called with an error-number argument and an error-string argument. These two arguments are passed from the set_error_handler() call to our function.
There are more arguments we could use, as we will soon see, but the output of running this code would be.
Error: [8] Undefined variable: foo
Let’s see what a slightly less simple custom error handling function could look like. This error handler should be able to detect the error types and decide what action to take and/or what error message to show.
function errorHandler($errno, $errstr, $errfile, $errline) { switch ($errno) { case E_NOTICE: case E_USER_NOTICE: case E_DEPRECATED: case E_USER_DEPRECATED: case E_STRICT: echo("STRICT error $errstr at $errfile:$errline n"); break; case E_WARNING: case E_USER_WARNING: echo("WARNING error $errstr at $errfile:$errline n"); break; case E_ERROR: case E_USER_ERROR: case E_RECOVERABLE_ERROR: exit("FATAL error $errstr at $errfile:$errline n"); default: exit("Unknown error at $errfile:$errline n"); } } set_error_handler("errorHandler"); $foo = false; if (! $foo) { trigger_error('Value $foo must be true', E_USER_NOTICE); }
Note how we are grouping the handling of certain error types using the fall-through functionality of the switch-case statements.
The output would be something like:
STRICT error Value$foo must true at /tmp/test.php:32
Now we can handle types of errors differently (or the same) and show custom error message, too. Of course, a real custom error handling function should be able to do much more with errors and possibly act upon them in a specific and sensible way.
It is important to keep custom error handling function simple and robust to ensure no errors occur within this function itself, as those would not get reported. There are several PHP libraries that can help us for creating more advanced error handling.
Now days, Freelancing is simplest, easiest and best way to Hire PHP developers for full-time or part-time.
Once you've decided to hire dedicated PHP developer instantly, you need to follow some basic steps to Hire PHP developer for website design & development. The first place to start with is your outsourced project itself, define outline of your website concept with complete requirement and summarizing the vital elements and analyze your requirement for determine the employment type (individual/agency), budget and share your requirement on freelance platform. Within 24 hours you get top PHP developers proposals, you need to scan their portfolio and shortlisted them, preferred only experienced PHP developer because hiring experience person is always better deals. And finally invite them for interview to assess their abilities. If you aren't from a technical background, seek someone proficient to conduct a technical interview of PHP developers.
Hire PHP developer to build a website as per the requirement of your business in a convenient manner.
A team of professionals can undertake the requirement of your business and can turn your ideas into reality.
Hiring PHP experts is a must for you if you want to build a best PHP based site with a professional look. Top Web developers from all over the world are opting for this software to build a site because of its cost effectiveness, flexibility, simplicity and free availability. Top PHP developers for hire to accomplish your business projects.
If you decide to create a website in PHP and you don't know PHP programming and website design. Freelance market is best choice for development of an attractive and fully functional website. You just focus on your business and define the primary purpose of your website, your specific user experience and fundamental goal of your website. Following points are step by step guide for creating a website for business.
PHP (Hypertext Pre-processor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
PHP Programming Languages most commonly used for web site and web Application Development. PHP could be use for a general purpose, server-side scripting language run a web server that is designed to form dynamic pages and applications.
PHP as a web development option is secure, quick and a reliable that gives lots more advantages to form it accessible to a lot of people. It is to be given a thought on what has made PHP Programming as one of the most commonly programming language for the online business.
Common mistake that need to be avoided while hiring a PHP developer