How do I get PHP errors to display?

PhpError HandlingSyntax ErrorError Reporting

Php Problem Overview


I have checked my PHP ini file (php.ini) and display_errors is set and also error reporting is E_ALL. I have restarted my Apache webserver.

I have even put these lines at the top of my script, and it doesn't even catch simple parse errors. For example, I declare variables with a "$" and I don't close statements ";". But all my scripts show a blank page on these errors, but I want to actually see the errors in my browser output.

error_reporting(E_ALL);
ini_set('display_errors', 1);

What is left to do?

Php Solutions


Solution 1 - Php

This always works for me:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:

display_errors = on

(if you don't have access to php.ini, then putting this line in .htaccess might work too):

php_flag display_errors 1

Solution 2 - Php

You can't catch parse errors when enabling error output at runtime, because it parses the file before actually executing anything (and since it encounters an error during this, it won't execute anything). You'll need to change the actual server configuration so that display_errors is on and the approriate error_reporting level is used. If you don't have access to php.ini, you may be able to use .htaccess or similar, depending on the server.

This question may provide additional info.

Solution 3 - Php

Inside your php.ini:

display_errors = on

Then restart your web server.

Solution 4 - Php

To display all errors you need to:

1. Have these lines in the PHP script you're calling from the browser (typically index.php):

error_reporting(E_ALL);
ini_set('display_errors', '1');

2.(a) Make sure that this script has no syntax errors

—or—

2.(b) Set display_errors = On in your php.ini

Otherwise, it can't even run those 2 lines!

You can check for syntax errors in your script by running (at the command line):

php -l index.php

If you include the script from another PHP script then it will display syntax errors in the included script. For example:

index.php

error_reporting(E_ALL);
ini_set('display_errors', '1');

// Any syntax errors here will result in a blank screen in the browser

include 'my_script.php';

my_script.php

adjfkj // This syntax error will be displayed in the browser

Solution 5 - Php

Some web hosting providers allow you to change PHP parameters in the .htaccess file.

You can add the following line:

php_value display_errors 1

I had the same issue as yours and this solution fixed it.

Solution 6 - Php

You might find all of the settings for "error reporting" or "display errors" do not appear to work in PHP 7. That is because error handling has changed. Try this instead:

try{
     // Your code
} 
catch(Error $e) {
    $trace = $e->getTrace();
    echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().' called from '.$trace[0]['file'].' on line '.$trace[0]['line'];
}

Or, to catch exceptions and errors in one go (this is not backward compatible with PHP 5):

try{
     // Your code
} 
catch(Throwable $e) {
    $trace = $e->getTrace();
    echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().' called from '.$trace[0]['file'].' on line '.$trace[0]['line'];
}

Solution 7 - Php

This will work:

<?php
     error_reporting(E_ALL);
     ini_set('display_errors', 1);    
?>

Solution 8 - Php

Use:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

This is the best way to write it, but a syntax error gives blank output, so use the console to check for syntax errors. The best way to debug PHP code is to use the console; run the following:

php -l phpfilename.php

Solution 9 - Php

Set this in your index.php file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Solution 10 - Php

Create a file called php.ini in the folder where your PHP file resides.

Inside php.ini add the following code (I am giving an simple error showing code):

display_errors = on

display_startup_errors = on

Solution 11 - Php

I would usually go with the following code in my plain PHP projects.

if(!defined('ENVIRONMENT')){
    define('ENVIRONMENT', 'DEVELOPMENT');
}

$base_url = null;

if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'DEVELOPMENT':
            $base_url = 'http://localhost/product/';
            ini_set('display_errors', 1);
            ini_set('display_startup_errors', 1);
            error_reporting(E_ALL|E_STRICT);
            break;

        case 'PRODUCTION':
            $base_url = 'Production URL'; /* https://google.com */
            error_reporting(0);
            /* Mechanism to log errors */
            break;

        default:
            exit('The application environment is not set correctly.');
    }
}

Solution 12 - Php

As we are now running PHP 7, answers given here are not correct any more. The only one still OK is the one from Frank Forte, as he talks about PHP 7.

On the other side, rather than trying to catch errors with a try/catch you can use a trick: use include.

Here three pieces of code:

File: tst1.php

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    // Missing " and ;
    echo "Testing
?>

Running this in PHP 7 will show nothing.

Now, try this:

File: tst2.php

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    include ("tst3.php");
?>

File: tst3.php

<?php
    // Missing " and ;
    echo "Testing
?>

Now run tst2 which sets the error reporting, and then include tst3. You will see:

> Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in tst3.php on line 4

Solution 13 - Php

If, despite following all of the above answers (or you can't edit your php.ini file), you still can't get an error message, try making a new PHP file that enables error reporting and then include the problem file. eg:

error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('problem_file.php');

Despite having everything set properly in my php.ini file, this was the only way I could catch a namespace error. My exact scenario was:

//file1.php
namespace a\b;
class x {
    ...
}

//file2.php
namespace c\d;
use c\d\x; //Dies because it's not sure which 'x' class to use
class x {
    ...
}

Solution 14 - Php

If you somehow find yourself in a situation where you can't modifiy the setting via php.ini or .htaccess you're out of luck for displaying errors when your PHP scripts contain parse errors. You'd then have to resolve to linting the files on the command line like this:

find . -name '*.php' -type f -print0 | xargs -0 -n1 -P8 php -l | grep -v "No syntax errors"

If your host is so locked down that it does not allow changing the value via php.ini or .htaccess, it may also disallow changing the value via ini_set. You can check that with the following PHP script:

<?php
if( !ini_set( 'display_errors', 1 ) ) {
  echo "display_errors cannot be set.";
} else {
  echo "changing display_errors via script is possible.";
}

Solution 15 - Php

You can do something like below:

Set the below parameters in your main index file:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);

Then based on your requirement you can choose which you want to show:

For all errors, warnings and notices:

    error_reporting(E_ALL); OR error_reporting(-1);

For all errors:

    error_reporting(E_ERROR);

For all warnings:

    error_reporting(E_WARNING);

For all notices:

    error_reporting(E_NOTICE);

For more information, check here.

Solution 16 - Php

You can add your own custom error handler, which can provide extra debug information. Furthermore, you can set it up to send you the information via email.

function ERR_HANDLER($errno, $errstr, $errfile, $errline){
    $msg = "<b>Something bad happened.</b> [$errno] $errstr <br><br>
    <b>File:</b> $errfile <br>
    <b>Line:</b> $errline <br>
    <pre>".json_encode(debug_backtrace(), JSON_PRETTY_PRINT)."</pre> <br>";

    echo $msg;

    return false;
}

function EXC_HANDLER($exception){
    ERR_HANDLER(0, $exception->getMessage(), $exception->getFile(), $exception->getLine());
}

function shutDownFunction() {
    $error = error_get_last();
    if ($error["type"] == 1) {
        ERR_HANDLER($error["type"], $error["message"], $error["file"], $error["line"]);
    }
}

set_error_handler ("ERR_HANDLER", E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
register_shutdown_function("shutdownFunction");
set_exception_handler("EXC_HANDLER");

Solution 17 - Php

This code on top should work:

error_reporting(E_ALL);

However, try to edit the code on the phone in the file:

error_reporting =on

Solution 18 - Php

The best/easy/fast solution that you can use if it's a quick debugging, is to surround your code with catching exceptions. That's what I'm doing when I want to check something fast in production.

try {
    // Page code
}
catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Solution 19 - Php

    <?php
    // Turn off error reporting
    error_reporting(0);
    
    // Report runtime errors
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
    
    // Report all errors
    error_reporting(E_ALL);
    
    // Same as error_reporting(E_ALL);
    ini_set("error_reporting", E_ALL);
    
    // Report all errors except E_NOTICE
    error_reporting(E_ALL & ~E_NOTICE);
    ?>

While your site is live, the php.ini file should have display_errors disabled for security reasons. However, for the development environment, display_errors can be enabled for troubleshooting.

Solution 20 - Php

Just write:

error_reporting(-1);

Solution 21 - Php

You can do this by changing the php.ini file and add the following

display_errors = on
display_startup_errors = on

OR you can also use the following code as this always works for me

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Solution 22 - Php

If you have Xdebug installed you can override every setting by setting:

xdebug.force_display_errors = 1;
xdebug.force_error_reporting = -1;

> force_display_errors > > Type: int, Default value: 0, Introduced in Xdebug >= 2.3 If this > setting is set to 1 then errors will always be displayed, no matter > what the setting of PHP's display_errors is. > force_error_reporting

> Type: int, Default value: 0, Introduced in Xdebug >= 2.3 This setting is a bitmask, like error_reporting. This bitmask will be logically ORed with the bitmask represented by error_reporting to dermine which errors should be displayed. This setting can only be made in php.ini and allows you to force certain errors from being shown no matter what an application does with ini_set().

Solution 23 - Php

You might want to use this code:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Solution 24 - Php

If it is on the command line, you can run php with -ddisplay_errors=1 to override the setting in php.ini:

php -ddisplay_errors=1 script.php

Solution 25 - Php

Report all errors except E_NOTICE

error_reporting(E_ALL & ~E_NOTICE);

Display all PHP errors

error_reporting(E_ALL);  or ini_set('error_reporting', E_ALL);

Turn off all error reporting

error_reporting(0);

Solution 26 - Php

     error_reporting(1);
     ini_set('display_errors', '1');
     ini_set('display_startup_errors', '1');
     error_reporting(E_ALL);

Put this at the top of your page.

Solution 27 - Php

Input this on the top of your code

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

And in the php.ini file, insert this:

display_errors = on

This must work.

Solution 28 - Php

Accepted asnwer including extra options. In PHP files for in my DEVELOPMENT apache vhost (.htaccess if you can ensure it doesn't get into production):

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:

display_errors = on

(if you don't have access to php.ini, then putting this line in .htaccess might work too):

// I've added some extra options that set E_ALL as per https://www.php.net/manual/en/errorfunc.configuration.php.
php_flag log_errors on
php_flag display_errors on
php_flag display_startup_errors on
php_value error_reporting 2147483647
php_value error_log /var/www/mywebsite.ext/logs/php.error.log

Solution 29 - Php

In Unix CLI, it's very practical to redirect only errors to a file:

./script 2> errors.log

From your script, either use var_dump() or equivalent as usual (both STDOUT and STDERR will receive the output), but to write only in the log file:

fwrite(STDERR, "Debug infos\n"); // Write in errors.log^

Then from another shell, for live changes:

tail -f errors.log

or simply

watch cat errors.log

Solution 30 - Php

You can show Php error in your display via simple ways. Firstly, just put this below code in your php.ini file.

display_errors = on;

(if you don't have access to php.ini, then putting this line in .htaccess might work too):

php_flag display_errors 1

OR you can also use the following code in your index.php file

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Solution 31 - Php

If you are on a SharedHosting plan (like on hostgator)... simply adding

php_flag display_errors 1

into a .htaccess file and uploading it to the remote folder may not yield the actual warnings/errors that were generated on the server.

What you will also need to do is edit the php.ini

> This is how you do it via cPanel (tested on hostgator shared hosting > plan)

After logging into your cPanel, search for MultiPHP INI Editor. It is usually found under the SOFTWARE section in your cPanel list of items.

On the MultiPHP INI Editor page ...you can stay on the basic mode tab and just check the button on the line that says display_errors. Then click the Apply button to save.

enter image description here

> IMPORTANT: Just remember to turn it back off when you are done debugging; because this is not recommended for public servers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbsView Question on Stackoverflow
Solution 1 - PhpFancy JohnView Answer on Stackoverflow
Solution 2 - PhpMichael MadsenView Answer on Stackoverflow
Solution 3 - Phpuser1803477View Answer on Stackoverflow
Solution 4 - PhpandreView Answer on Stackoverflow
Solution 5 - PhpKalhuaView Answer on Stackoverflow
Solution 6 - PhpFrank ForteView Answer on Stackoverflow
Solution 7 - PhpMahendra JellaView Answer on Stackoverflow
Solution 8 - PhpAbhijit JagtapView Answer on Stackoverflow
Solution 9 - PhpSumit GuptaView Answer on Stackoverflow
Solution 10 - PhpNavyaKumarView Answer on Stackoverflow
Solution 11 - PhpChannaveer HakariView Answer on Stackoverflow
Solution 12 - PhpPeterView Answer on Stackoverflow
Solution 13 - PhpjxmallettView Answer on Stackoverflow
Solution 14 - PhpchiborgView Answer on Stackoverflow
Solution 15 - PhpBinit GhetiyaView Answer on Stackoverflow
Solution 16 - PhplintabáView Answer on Stackoverflow
Solution 17 - PhpJoel WemboView Answer on Stackoverflow
Solution 18 - PhpXakiruView Answer on Stackoverflow
Solution 19 - PhppardeepView Answer on Stackoverflow
Solution 20 - PhpjewelhuqView Answer on Stackoverflow
Solution 21 - PhpMuhammad Adeel MalikView Answer on Stackoverflow
Solution 22 - PhpPeter HaberkornView Answer on Stackoverflow
Solution 23 - Phpuser8031209View Answer on Stackoverflow
Solution 24 - PhpgvlasovView Answer on Stackoverflow
Solution 25 - PhpShaikh NadeemView Answer on Stackoverflow
Solution 26 - PhpKwedView Answer on Stackoverflow
Solution 27 - PhpropehapiView Answer on Stackoverflow
Solution 28 - Phpwebcoder.co.ukView Answer on Stackoverflow
Solution 29 - PhpNVRMView Answer on Stackoverflow
Solution 30 - PhpDevsaifulView Answer on Stackoverflow
Solution 31 - PhpReally Nice CodeView Answer on Stackoverflow