PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP Error Types

In PHP, different types of errors can occur during script execution. Understanding these types is crucial for effective debugging and error handling. Here are the primary types of errors you might encounter in PHP:

1. Parse Errors (Syntax Errors)

Parse errors occur when there's a syntax mistake in your script, and the parser fails to compile it. This could be due to incorrect closing of braces, missing semicolons, incorrect function names, etc.

Example:

<?php
    echo "Hello, World!"
?>

In this example, a semicolon is missing at the end of the echo statement, which will result in a parse error.

2. Fatal Errors

Fatal errors are critical errors - for example, instantiating an object of a non-existent class, calling a non-existent function, or using require() to include a non-existent file. These errors cause the immediate termination of the script.

Example:

<?php
    function testFunction() {
        echo "Hello, World!";
    }

    // Call to undefined function
    testFunc();
?>

In this example, calling testFunc() results in a fatal error, as the function doesn't exist.

3. Warning Errors

Warning errors are more serious than Notices but less severe than Fatal errors. They do not halt script execution. A common example would be include() a file that does not exist.

Example:

<?php
    // File does not exist
    include("non_existent_file.php");
?>

In this example, trying to include a file that doesn't exist will result in a warning, but the script will continue to run.

4. Notice Errors

Notice errors are minor, non-critical errors that PHP encounters while executing a script - for example, accessing an undefined variable. These errors do not halt script execution but may indicate possible bugs.

Example:

<?php
    // Undefined variable
    echo $undefined_variable;
?>

In this example, trying to echo an undefined variable will result in a notice, but the script will continue to run.

5. Deprecated Errors

Deprecated errors occur when you use code that is outdated and might not be supported in future versions of PHP. For example, using the mysql_connect() function in PHP 7 will result in a deprecated error.

Example:

<?php
    // Deprecated function
    $link = mysql_connect("localhost", "mysql_user", "mysql_password");
?>

In this example, the mysql_connect() function is deprecated in PHP 7, and trying to use it will result in a deprecated error.

These are the primary error types you'll encounter in PHP. For effective debugging, it's recommended to have error reporting turned on during development to understand and fix issues in the code, and then turn off display errors when moving to production for security and user experience.

  1. Handling PHP E_WARNING errors: Handle E_WARNING errors using a custom error handler:

    <?php
    function customErrorHandler($errno, $errstr, $errfile, $errline) {
        echo "Warning: [$errno] $errstr in $errfile on line $errline\n";
    }
    
    set_error_handler("customErrorHandler");
    
    // Trigger a warning
    $result = fopen('nonexistent_file.txt', 'r');
    
  2. Handling PHP E_NOTICE errors: Handle E_NOTICE errors using a custom error handler:

    <?php
    function customErrorHandler($errno, $errstr, $errfile, $errline) {
        echo "Notice: [$errno] $errstr in $errfile on line $errline\n";
    }
    
    set_error_handler("customErrorHandler");
    
    // Trigger a notice
    $undefinedVariable = $nonexistentVariable;
    
  3. PHP E_PARSE error type and troubleshooting: E_PARSE errors occur during the parsing phase. Troubleshoot syntax errors:

    <?php
    // Trigger a parse error
    echo "This is a syntax error";
    
  4. PHP E_DEPRECATED and handling deprecated code: Handle E_DEPRECATED errors and update deprecated code:

    <?php
    // Handle deprecated code
    function deprecatedFunction() {
        trigger_error("This function is deprecated", E_DEPRECATED);
    }
    
    deprecatedFunction();
    
  5. Suppressing PHP E_STRICT warnings: Suppress E_STRICT warnings using the @ symbol:

    <?php
    // Suppress E_STRICT warnings for this line
    @mysqli_query($conn, 'SELECT * FROM table');
    
  6. PHP E_COMPILE_ERROR and E_COMPILE_WARNING: E_COMPILE_ERROR and E_COMPILE_WARNING occur during compilation. Troubleshoot compilation issues:

    <?php
    // Trigger a compile error
    echo "This is a compile error";
    
  7. PHP E_USER_ERROR and custom error messages: Generate E_USER_ERROR with a custom error message:

    <?php
    // Trigger a user-generated error
    trigger_error("This is a user-generated error", E_USER_ERROR);
    
  8. PHP E_USER_WARNING for user-generated warnings: Generate E_USER_WARNING with a custom warning message:

    <?php
    // Trigger a user-generated warning
    trigger_error("This is a user-generated warning", E_USER_WARNING);
    
  9. Using PHP E_USER_NOTICE for custom notices: Generate E_USER_NOTICE with a custom notice message:

    <?php
    // Trigger a user-generated notice
    trigger_error("This is a user-generated notice", E_USER_NOTICE);
    
  10. PHP E_USER_DEPRECATED for user-deprecated code: Generate E_USER_DEPRECATED for code that is deprecated by the user:

    <?php
    // Trigger a user-deprecated code notice
    trigger_error("This code is deprecated", E_USER_DEPRECATED);
    
  11. PHP error_reporting() for configuring error types: Configure error types using error_reporting():

    <?php
    // Configure error reporting to include warnings and errors
    error_reporting(E_ALL & ~E_NOTICE);