PHP Programming Error Types
Programming errors usually stay unobserved till the program is compiled or executed. A number of the errors inhibit the program from getting compiled or executed. So errors ought to be removed before compiling and executing.
There are three types of programming errors in PHP -
- Syntax Errors
- Runtime Errors
- Logical Errors
Syntax Errors
PHP language has a set of rules called the syntax, which statements must follow to be valid. If a statement does not follow the rules of a language, it is said to be a syntax error. Syntax errors are often called parse errors in a PHP like interpreted language. Syntax Errors stop the execution of the script.
Example-<?php
$var = sample';
?>
In the above code, we have missed the opening quotation mark.
This script generates the following error message:
Parse error: syntax error, unexpected ''; ' (T_ENCAPSED_AND_WHITESPACE) in parse_error.php on line 2
Runtime Errors
Runtime errors can be harder to detect and fix. Like include a file that does not exist, calls to a function that does not exist, failure to check input data, reading or writing to a file that does not exist. This errors stop the execution of the script.
Example-<?php
require('file.php');
?>
If the required file 'file.php' does not exist, then this generates a runtime error.
This script generates the following error message at runtime -
Fatal error: require(): Failed opening required 'file.php' (include_path='.;C:\php\pear') in runtime_error.php on line 2
Logical Errors
Logical errors can be a harder type of error to find and eliminate. This type of error occurs when perfectly valid code does exactly what it is instructed to do, but that was not what the writer intended. This error does not terminate the execution of the script.
Example-<?php
if($i = 1) {
echo 'Value is equal to One';
}
?>
The above snippet of the code is perfectly valid. This compile and run successfully without any error message but does not do right thing.
PHP Connection and File Handling on FTP Server
File Upload Validation in PHP