Right now my pages look something like this:
if($_GET[\'something\'] == \'somevalue\')
{
$output .= \'somecode\';
// make a DB query, fetch a row
Handle PHP error and warning in correctly by using error handling functions. ( See example here )
Best way to Error handling in PHP is, You can stop all error reporting by adding this line in top of your php file -
error_reporting(0);
//OR
error_reporting('E_ALL');
// Predefined Constant
Error handling in PHP using functions:
All functions which is listed above are used for Error handling in PHP.
PDO error exception handling for queries, and really all code should be run through:
try{
}
catch{
}
finally{
}
The reason for this, is it makes debugging much easier when you can pinpoint roughly where in lengthy scripts an error is occuring
more info here: http://php.net/manual/en/language.exceptions.php
There are a lot of ways that you can deal with this and frankly none of them is intrinsically 'right'.
You will have to decide for yourself, which method is more 'comfortable' for you - it's always a mater of preferences (although there are certain techniques you should avoid and for good reasons).
It will highly depend on how you split your logic, however I tend to enclose all code that can return non-fatal errors inside a function, and use a return value of said function to indicate there was an error.
For fatal errors I tend to use exceptions (with try-catch
blocks).
Now just to be clear:
NTP
protocol, but the server didn't respond, you can decide to use local time
function and still display a some valuable data to the user.SQL Exception
- there is no valuable data to be shown and you can only inform the user of this.A good example of using function-returns as a way of dealing with non-fatal problems would be a function that is trying to display content of some file on the page when this is not the main objective of the page (for example you would have a function that displays badges, fetched from a text file, on every single page - I know that this is far fetched but bear with me).
function getBadge($file){
$f = fopen($file,'r');
if(!$f){
return null;
}
.. do some processing ..
return $badges;
}
$badges = getBadges('badges.txt');
if(!$badges){
echo "Cannot display badges.";
} else {
echo $badges;
}
.. carry on doing whatever page should be doing ..
In fact, the function fopen
itself is an example of this - it will return.
Returns a file pointer resource on success, or FALSE on error.
When you have some piece of code that needs to be executed because it's exactly what the user wanted (for example reading all news from database and displaying them to the user), you could use exceptions. Let's take a simple example - a user visited his profile and wanted to see all the messages he's got (let's assume, for now, that they are stored in plain text). You might have a function like:
function getMessages($user){
$messages = array();
$f = fopen("messages_$user.txt","r");
if(!$f){
throw new Exception("Could not read messages!");
}
... do some processing ...
return $messages;
}
And use it like this:
try{
..do some stuff..
$messages = getMessages($_SESSION['user'])); //assuming you store username in $_SESSION
foreach($messages as $msg){
echo $msg."<br/>";
}
} catch(Exception $e){
echo "Sorry, there was an error: ".$e->getMessage();
}
Now this could come in handy, if you had a 'top-level' script that would execute all the other code. That means that, for example, in your index.php
you would just have:
try{
.. execute some code, perform some functions ..
} catch(Exception $e){
echo "Sorry, there was an error: ".$e->getMessage();
}
Whatever you do, never use exceptions as a way to check something you can recover from. Have a read on another question(full credit goes to Anton Gogolev for a very good explanation on this, as well as other answer-ers) as to why this is the case.
Now there is no better way to learn how to deal with errors than to try several things and see what is good for you. You might find the below useful:
trigger_error()
function, which I haven't mentioned because I don't use it and don't know much about it, but apparently it's really useful. This is a particularly good read.Hope this helps :)
Using try-catch
is one of the cleanest solutions you can use.
I have made an example that still displays the header and footer when an error occurs, using your code converted to the try-catch
format:
PHP:
<?php
try {
$output = array();
if($_GET['something'] != 'somevalue') throw new Exception('something does not have a valid value.');
$output[] = 'Some Code';
$row = mt_rand(0, 10) < 5 ? null : mt_rand(0, 100);
if($row === null) throw new Exception('The row does not exist.');
$output[] = $row;
if(!somethingIsOK()) throw new Exception('Something is most definitely not OK.');
$output[] = 'Yet more page output';
} catch(Exception $e) {
$output[] = 'Error: ' . $e->getMessage(); // To show output and error
$output = array('Error: ' . $e->getMessage()); // To only show error
}
function somethingIsOK() {
return mt_rand(0, 10) < 5;
}
?>
HTML:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>PHP Error test</title>
<style type="text/css">
body {
background: #eee;
text-align: center
}
#content {
padding: 60px
}
#header {
padding: 30px;
background: #fff
}
#footer {
padding: 10px;
background: #ffffd
}
</style>
</head>
<body>
<div id="header">Header</div>
<div id="content">
<?php echo implode('<br />', $output); ?>
</div>
<div id="footer">Footer</div>
</body>
</html>
References:
Create error handler (set_error_handler) and throw exceptions inside it.
It will help for functions, that don't support exceptions.
If you're searching for a code structure which will look pretty and will work - you could use the whitelist
method I always use. For example - validating a $_GET
variable:
$error = false;
if(!isset($_GET['var']))
{
$error = 'Please enter var\'s value';
}
elseif(empty($_GET['var']))
{
$error = 'Var shouldn\'t be empty';
}
elseif(!ctype_alnum($_GET['var']))
{
$error = 'Var should be alphanumeric';
}
//if we have no errors -> proceed to db part
if(!$error)
{
//inserting var into database table
}
So, this is it , just 2 if/elseif
blocks, without nesting