Broad Network

PHP Error Basics

Basics of PHP – Part 15

Forward: In this part of the series, we look at the basics of errors.

By: Chrysanthus Date Published: 29 Jul 2012


This is part 15 of my series, Basics of PHP. In this part of the series, we look at the basics of errors.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at That is, contact me for the slightest problem you have about what you are reading.

Programming Errors
There are three types of programming errors. In other words, there are three types of errors that can occur in a program. You have Syntax Errors, Logic Errors and Runtime Errors.

Syntax Errors
This is the wrong use of syntax. These errors are wrong statements. When you type a statement, which is wrong, that is a syntax error. Such a statement cannot be executed. For example, in a statement you can type a variable without the $ sign. Under this condition, your program does not work. Depending on how you configure your PHP installation, such an error might be indicated by PHP to the output device just before the program is to be executed, when you give a command to run the program. With a syntax error, the program is not executed. Before PHP code is executed, there is some minimum compilation that takes place. Syntax errors would be spotted by the PHP compiler and reported, and execution (interpretation) of the program will not take place.

Logic Errors
In this case, PHP interpreter understands your program very well and it executes the program. However, the program will not do what you wanted it to do. It will do something slightly different or completely different. The fault is yours. For example, a loop that is required to do 10 iterations might do 5 iterations, because you coded it mistakenly to do 5 iterations. Another example is that a loop might iterate infinitely, because the condition you gave for the loop is wrong. Logic Errors occur when the program is being executed (interpreted). The only way to solve this problem is to test your program very well before you hand it to the customer (who asked for it).

Runtime Errors
Runtime errors occur when the program is being executed as a result of the fact that you did not take certain factor into consideration when coding. For example, let us say your code is to divide 8 by some denominator that the user inputs. If the user inputs 2, the division will work, giving you 4 as answer. If the user inputs zero, the division will not work, because 8/0 is undefined. When a runtime error occurs your program normally crashes (and stop). To solve runtime errors, you have to write code that will prevent the execution of the particular code segment from taking place. In this division example, you have to write code that will prevent division by zero from taking place, and possibly informing the user of the mistake he made by inputting zero as a denominator.

Preventing Runtime Errors
Runtime errors are prevented using what is called try-catch blocks. We shall use the example of dividing 8 by a denominator to illustrate this. Consider the following code:


        $input = 2;

                if ($input == 0)
                    throw new Exception('Division by zero is not allowed.');
                $var = 8 / $input;
                echo $var;
        catch (Exception $e)
                echo "Error: ",  $e->getMessage();


Try the above code. You should have 4 as the answer, displayed. Now change the value of the $input variable at the beginning of the code to 0. You should have the text, “Error: Division by zero is not allowed.” displayed.

The code divides 8 by the value of the $input variable. When the value of the $input variable is not zero, everything is fine. When the value is zero, that is an error, and so the program should not crash. Code has to be written to prevent the program from crashing.  

There are four things you should note about the code above. There is the try-block; there is the catch-block; there is an object created from a class called Exception; and there is a statement called the throw statement around the beginning of the try block.

The try-block begins with the reserved word, try, then you have the pair of curly braces. The statements for the try block are in the curly braces. The catch-block begins with the reserved word, catch. It has parentheses with a parameter. The parameter is a variable, preceded by the variable type (we have not seen this variable type before). The catch-block has a pair of curly braces. The statements for the catch block go into the curly braces.

The very first statement in the try-block is an if-statement combined with what is called the throw statement? The idea is that you check if the $input variable is zero. If it is zero, then the throw statement is executed to prevent crash. When the throw statement is executed, we say an exception is thrown. When an exception is thrown, the statements below the throw statement in the try block are not executed; while the statements in the catch-block must be executed; that is, when an error occurs, the statements below the throw statement in the try-block are not executed, while the catch-block must be executed to handle the problem. If no error occurs (in this case, input is not zero), then an exception will not be thrown. If an exception is not thrown, the statements below the throw statements in the try-block are executed, and the catch-block is not executed.

The try-block has the normal statements to be executed to solve task required by the program. These statements are executed on condition that no error has occurred. The catch block has the statements to be executed if an error occurs. Usually what the catch block does is that it simply informs the user that an error has occurred with a brief description of the error. If the error is detected in the try block and the catch block is executed as in the above code, then the program will not crash. I repeat, usually, the catch-block does not do much more than display a brief error message to the user. Prevention of crash is as a result of the fact that the normal statements in the try block are not executed and the catch-block is executed.

Introduction to the Exception Class
There is a class called the Exception class. This class has a good number of members and methods. For this basic tutorial we shall talk about just one of its members. The member holds the error message you want to give to the user. You, the programmer, are the one who decides on the error message. You feed in the error message when you create an object from the class. You create an object at the same time that you are throwing the exception. In the above code, we have,

    throw new Exception('Division by zero is not allowed.')

Here, the reserved word, throw, is to throw the exception. The exception is an object, which can have the error message and other things. In this tutorial, we consider only the error message. To create an exception object, we begin with the operator, new. This is followed, by the name of the class, Exception; then parentheses. In the parentheses, you type the error message in quotes. The Exception class has a constructor method that assigns this error message as value to one of its members (one of its variables). This exception object is thrown to be caught be a catch-block. So the if-statement detects the error, throws an exception object, which knows the error and the catch-block catches the exception object. The catch-block uses information in the exception object to complete the error handling.

The catch-block
The catch is like a function. It has parentheses, which has a parameter. The parameter is the variable for an exception object. You choose whatever name you want for the variable, and precede it with the $ sign. You precede the variable with the word, Exception. This means the type of variable is an exception. The throw-statement is like a call to the catch-block.

In the above code, we have the echo statement. The echo construct takes a comma-separated list of arguments. In this case, the first argument is the string "Error: ". This string will be displayed first at the browser. This string indicates to the user that there is an error. The description of the error follows from the next argument. This next argument to the echo construct is a return value of a method of the exception object. What we have exactly is:


$e is the catch parameter variable, which is the object caught that was thrown. getMessage() is the method to return the error message that we typed when creating the object. This is the only exception class method we consider in this tutorial.

Some Comments
There is an old way of dealing with runtime errors in PHP; I will not discuss that. There are also predefined exceptions; I will also not discuss that for this basic tutorial. What I have given you in this tutorial is enough to get you going in PHP programming.

The NULL Value
This section is not really dealing with errors, but is sometimes associated with errors. The reserved word, NULL, whose letters can be in any case (upper or lower) can be assigned to a variable, like in,

    $someVar = null;

When a variable is declared without any value assigned to it, the variable is considered as NULL.

Let us stop here and continue in the next part of the series.


Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course


Become the Writer's Fan
Send the Writer a Message