PHP Errors

You’ll know what a PHP error is the second you see it. If any part of your PHP code is not formatted properly, your browser will display an error message.

It will be extremely difficult for you to correct the error if you don’t know how to read and interpret the error that is shown.

You can also create custom errors which you define and are displayed when a user runs into a dead end.

die() errors

The die() error is the simplest way to display an error message, and is often use when attempting to connect to a database.

An example of using a die() error can look like:

$localhost =’localhost’;
$username =’username’;
$password =’password’;

$link = mysql_connect(“$localhost”, “$username”, “$password”)or die(“Could not connect to database!”);

In the above example, a connection to the database is attempted using the assigned variables for $localhost, $username and $password. If any of the three variables are incorrect, the die() error will display the error message:

Could not connect to the database!

Error Triggers And Handling

Creating custom error messages depending on the trigger helps you to pinpoint exactly which error occurred. Custom error triggers are created by using the trigger_error function.

An example of using a trigger_error can look like:

$age=18;
if ($age<18)
{
trigger_error(“You must be at least 18 years old to view this page.”);
}

This bit of code would display the error message “You must be at least 18 years old to view this page.” if the $age variable is less than 18.

The complete error message would look something like:

Notice: You must be at least 18 years old to view this page.
in agecheck.php on line 15

This custom error trigger and message will help you to identify that the error is caused due to the variable $age, which is set in the file agecheck.php on line 15.