PHP Error handling

This page discusses - PHP Error handling

PHP Error handling

PHP Error handling:

PHP provides so many techniques to handle error, if error occurs an error message is send to the browser.

Error handling should be the main concern whenever we develop any web application. If we do not take these things under consideration then we might develop an unprofessional web application.

In this tutorial you will come to know about different handling methods:

  • die() statement

We will see that what will be the output if we use die statement and don't.

Let's see what will be the output if we do not use die statement and try to connect with a MySQL server with an invalid user name:

Example:

<?php

$var=mysql_connect("localhost","rose","");

?>

Output:

Warning: mysql_connect() [function.mysql-connect]:
 Access denied for user 'rose'@'localhost' (using password: NO)
in C:\wamp\www\php\temp.php on line 2

Now we will examine what happen if we use die() statement.

Example:

<?php

$var=mysql_connect("localhost","rose","");

if(!($var))

{

die("Error".mysql_error());

}

else

{

echo("Welcome");

}

?>

 

Output:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'rose'@'localhost' (using password: NO) in C:\wamp\www\php\temp.php on line 2
ErrorAccess denied for user 'rose'@'localhost' (using password: NO)

As we can see that die() statement generates a message regarding the error.

  0