What is a php error?

Basically, an error is a mistake in a program that may be caused by writing incorrect syntax or incorrect code. An error message is displayed on your browser containing the filename along with location, a message describing the error, and the line number in which error has occurred.

There are usually different types of error. In PHP, mainly four types of errors are considered:

  1. Syntax Error or Parse Error
  2. Fatal Error
  3. Warning Error
  4. Notice Error

We will discuss all these errors in detail with examples:

Syntax Error or Parse Error

A syntax error is a mistake in the syntax of source code, which can be done by programmers due to their lack of concern or knowledge. It is also known as Parse error. Compiler is used to catch the syntax error at compile time.

Note: Syntax error stop the execution of the code.

These errors can occur due to these common reasons like unclosed quotes, missing semicolon, extra or missing parentheses, or unclosed brackets and many more. While compiling the program, syntax error can be caught by the compiler. It gives a parse error or syntax error message.

Example 1: Missing semicolon

Output

Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';' in C:\xampp\htdocs\program\fatalerror.php on line 5

Explanation: In this above example, a semicolon (;) was missing in line 5. So, it generated a parse error and displayed an error message on browser as given in the output.

Example 2: Missing dollar symbol

Output

Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\program\fatalerror.php on line 5

Explanation: In this above example, dollar ($) symbol was missing in line 5. So, it generated a parse error and displayed an error message on browser as given in the output.

Fatal Error

A fatal error is another type of error, which is occurred due to the use of undefined function. The PHP compiler understands the PHP code but also recognizes the undefined function. This means that when a function is called without providing its definition, the PHP compiler generates a fatal error.

A fatal error is generated when a function is called without its definition. See the below example containing the fatal error -

Example: Calling undefined function

In the above code we have defined the add() function but called other function, which is catch_fatal_error(). Therefore, it generates a fatal error and print an error message on the browser as given below:

Output

Fatal error: Uncaught Error: Call to undefined function catch_fatal_error() in C:\xampp\htdocs\program\fatalerror.php:15 Stack trace: #0 {main} thrown in C:\xampp\htdocs\program\fatalerror.php on line 13

Warning Error

A warning is generated when the programmer tries to include a missing file. The PHP function calls that missing file which does not exist. The warning error does not stop/prevent the execution of the program.

The main reason behind generating a warning error is to pass an incorrect number of parameters to a function or to include a missing file.

Example: Include missing file

Output

Warning Error:
Warning: include(jtp.php): failed to open stream: No such file or directory in C:\xampp\htdocs\program\fatalerror.php on line 7

Warning: include(): Failed opening 'jtp.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\program\fatalerror.php on line 7

Explanation: In this example, we tried to include a file in our program, which does not exist. So, it generated a warning and displayed an error message.

Notice Error

Notice error is same as warning error. When program contains something wrong, the notice error occurs. But it allows/continue the execution of the program with a notice error. Notice error does not prevent the execution of the code. For example - access to undefined variable.

Generally, notice error occurs when we try to use or access a variable which is undefined. See the below example to understand it-

Example 2: Access undefined variable

Output

Airtel
Notice: Undefined variable: automobile in C:\xampp\htdocs\program\fatalerror.php on line 6

Explanation: In this above example, we were trying to use a variable $automobile, which was not defined. Therefore, it generated a notice "Undefined variable" and continued the execution of the program.



PHP Error Introduction

The error functions are used to deal with error handling and logging.

The error functions allow us to define own error handling rules, and modify the way the errors can be logged.

The logging functions allow us to send messages directly to other machines, emails, or system logs.

The error reporting functions allow us to customize what level and kind of error feedback is given.


Installation

The PHP error functions are part of the PHP core. No installation is required to use these functions.


Runtime Configuration

The behavior of the error functions is affected by settings in php.ini.

Errors and logging configuration options:

NameDefaultDescriptionChangeable
error_reporting NULL Sets the error reporting level (either an integer or  named constants) PHP_INI_ALL
display_errors "1" Specifies whether errors should be printed to the screen, or if they should be hidden from the user.
Note: This feature should never be used on production systems (only to support your development)
PHP_INI_ALL
display_startup_errors "0" Even when display_errors is on, errors that occur during PHP's startup sequence are not displayed
Note: It is strongly recommended to keep display_startup_errors off, except for debugging
PHP_INI_ALL
log_errors "0" Defines whether script error messages should be logged to the server's error log or error_log.
Note: It is strongly advised to use error logging instead of error displaying on production web sites
PHP_INI_ALL
log_errors_max_len "1024" Sets the maximum length of log_errors in bytes. The value "0" can be used to not apply any maximum length at all. This length is applied to logged errors, displayed errors, and also to $php_errormsg (available since PHP 4.3) PHP_INI_ALL
ignore_repeated_errors "0" Specifies whether to log repeated error messages. When set to "1" it will not log errors with repeated errors from the same file on the same line (available since PHP 4.3) PHP_INI_ALL
ignore_repeated_source "0" Specifies whether to log repeated error messages. When set to "1" it will not log errors with repeated errors from different files or source lines (available since PHP 4.3) PHP_INI_ALL
report_memleaks "1" If set to "1" (the default), this parameter will show a report of memory leaks detected by the Zend memory manager (available since PHP 4.3) PHP_INI_ALL
track_errors "0" If set to "1", the last error message will always be present in the variable $php_errormsg PHP_INI_ALL
html_errors "1" Turns off HTML tags in error messages PHP_INI_ALL
PHP_INI_SYSTEM in PHP <= 4.2.3.
xmlrpc_errors "0" Turns off normal error reporting and formats errors as XML-RPC error message (available since PHP 4.1) PHP_INI_SYSTEM
xmlrpc_error_number "0" Used as the value of the XML-RPC faultCode element (available since PHP 4.1) PHP_INI_ALL
docref_root "" (available since PHP 4.3) PHP_INI_ALL
docref_ext "" (available since PHP 4.3.2) PHP_INI_ALL
error_prepend_string NULL Specifies a string to output before an error message PHP_INI_ALL
error_append_string NULL Specifies a string to output after an error message PHP_INI_ALL
error_log NULL Specifies the name of the file where script errors should be logged. The file should be writable by the web server's user. If the special value syslog is used, the errors are sent to the system logger instead PHP_INI_ALL



PHP Error and Logging Functions

FunctionDescription
debug_backtrace() Generates a backtrace
debug_print_backtrace() Prints a backtrace
error_clear_last() Clears the last error
error_get_last() Returns the last error that occurred
error_log() Sends an error message to a log, to a file, or to a mail account
error_reporting() Specifies which errors are reported
restore_error_handler() Restores the previous error handler
restore_exception_handler() Restores the previous exception handler
set_error_handler() Sets a user-defined error handler function
set_exception_handler() Sets a user-defined exception handler function
trigger_error() Creates a user-level error message
user_error() Alias of trigger_error()

PHP Predefined Error and Logging Constants

ValueConstantDescription
1 E_ERROR Fatal run-time errors. Errors that cannot be recovered from. Execution of the script is halted
2 E_WARNING Run-time warnings (non-fatal errors). Execution of the script is not halted
4 E_PARSE Compile-time parse errors. Parse errors should only be generated by the parser
8 E_NOTICE Run-time notices. The script found something that might be an error, but could also happen when running a script normally
16 E_CORE_ERROR Fatal errors at PHP startup. This is like E_ERROR, except it is generated by the core of PHP
32 E_CORE_WARNING Non-fatal errors at PHP startup. This is like E_WARNING, except it is generated by the core of PHP
64 E_COMPILE_ERROR Fatal compile-time errors. This is like E_ERROR, except it is generated by the Zend Scripting Engine
128 E_COMPILE_WARNING Non-fatal compile-time errors. This is like E_WARNING, except it is generated by the Zend Scripting Engine
256 E_USER_ERROR Fatal user-generated error. This is like E_ERROR, except it is generated in PHP code by using the PHP function trigger_error()
512 E_USER_WARNING Non-fatal user-generated warning. This is like E_WARNING, except it is generated in PHP code by using the PHP function trigger_error()
1024 E_USER_NOTICE User-generated notice. This is like E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error()
2048 E_STRICT Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code (Since PHP 5 but not included in E_ALL until PHP 5.4)
4096 E_RECOVERABLE_ERROR Catchable fatal error. Indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle, the application aborts as it was an E_ERROR (Since PHP 5.2)
8192 E_DEPRECATED Run-time notices. Enable this to receive warnings about code that will not work in future versions (Since PHP 5.3)
16384 E_USER_DEPRECATED User-generated warning message. This is like E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error() (Since PHP 5.3)
32767 E_ALL Enable all PHP errors and warnings (except E_STRICT in versions < 5.4)



How do I fix PHP errors?

Editing the php..
Log into your cPanel..
Go to the File Manager. ... .
Find the “Error handling and logging” section in the php.ini. ... .
Next you can set the display_errors variable to On or Off to either show the errors on your website or not..

What is PHP error level?

Error Levels in PHP. Usually, whenever the PHP engine encounters a problem that prevents a script from running properly it generate an error message. There are sixteen different error levels and each level is represented by an integer value and an associated constant. Here's a list of error levels: Error Level.

Where are PHP errors?

The location of the error log file itself can be set manually in the php. ini file. On a Windows server, in IIS, it may be something like "'error_log = C:\log_files\php_errors. log'" in Linux it may be a value of "'/var/log/php_errors.