1976

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?

ezio4df
  • 3,541
  • 6
  • 16
  • 31
Abs
  • 56,052
  • 101
  • 275
  • 409
  • 10
    I've yet to nail down exactly why this works sometimes and not others, but for anyone wanting to quickly toggle errors in a php script (or enable them via a `$_REQUEST` parameter) these two lines will work most of the time. – brandonscript Oct 28 '13 at 20:15
  • well you can see details of the error by enabling xdebug from php ini file. – jewelhuq Jan 13 '16 at 10:14
  • 1
    Most specific editors / IDEs like e.g. Notepad++,Eclipse have builtin syntax check and highlighting, They will show you issues like you described. Please don't turn on the display of errors on a live system. Hackers will love this, because in most cases paths are shown. You can define error / exeception handler. In this handler you could log the issue and send a mail to the developer, so that he can fix it immediately when an issue occurs. see https://www.php.net/manual/en/function.set-error-handler.php and https://www.php.net/manual/en/function.set-exception-handler.php – Alexander Behling Dec 10 '20 at 08:08

27 Answers27

3547

DEV environment

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 occurred in the same file - 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

PROD environment

Note that above recommentdtion is only suitable for the dev environment. On a live site it must be

display_errors = off
log_errors = on

And then you'll be able to see all errors in the error log. See Where to find PHP error log

AJAX calls

In case of AJAX call, on a dev server open DevTools (F12), then Network tab. Then initiate the request which result you want to see, and it will appear in the Network tab. Click on it and then the Response tab. There you will see the exact output.
While on a live server just check the error log all the same.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Fancy John
  • 38,140
  • 3
  • 27
  • 27
  • 11
    Also note that you can use these 3 lines, and then include('fileImWorkingOn.php');. Then you can catch the syntax errors too! – Snap May 08 '15 at 18:11
  • 18
    While I'm no SysOps, I think more people have an .htaccess file than php.ini, and these would both come before parsing, right? `php_flag display_errors 1` for .htaccess – Ryan Taylor Jul 09 '15 at 21:58
  • 1
    So now that the errors get logged, where do they go? I went to /var/log/apache2 and it shows all the logs, but there is no information regarding the program I recently ran. I only get information about system restarts once every morning. – Michael May 17 '16 at 15:14
  • 1
    @Michael The errors go right to the screen or to where the output is redirected to – Fancy John May 18 '16 at 05:35
  • 3
    `E_ALL` isn't sufficient to display *all* errors in PHP 5.3. "`E_STRICT` became part of `E_ALL` in 5.4.0" - [PHP Manual](https://secure.php.net/function.error-reporting) You need `E_ALL | E_STRICT` or `-1` in that version. – Gerard Roche Sep 14 '16 at 04:12
  • 1
    For PHP7 this might not be good enough since error handling has changed. See my answer below about catching Errors. – Frank Forte Oct 15 '16 at 00:29
  • 2
    Everyone has a `PHP.ini`, Few have a `.htaccess` file ... The ability to edit their PHP.ini file is another thing.. And most public servers allow for a custom php.ini file to be used.. – Angry 84 Nov 10 '16 at 05:47
  • Parse errors will be displayed if error not in same file in which you turned on error reporting/displaying. You need to turn on them in first file of your project (f.e. `index.php`), and all syntax (parse) errors will be displayed (except errors in `index.php`). Otherwise you must turn on errors displaying in `.htaccess` (if server support it). – Taron Saribekyan Nov 20 '16 at 06:04
  • As suggested by @Snap that you can show parse errors on included/required files.. Basically have the above 3 lines enabled, then include your file... As long as the base PHP does not have errors, it will show parse errors for the included files. Very handy way to debug is by wrapping your code with a simple index file that only turns on errors and includes a sub file to do all the work. – Angry 84 Feb 05 '17 at 23:05
  • @RyanTaylor Adding `php_flag display_errors 1` to .htaccess doesn't do anything for me on PHP 5.6 – BadHorsie Feb 27 '17 at 18:18
  • @BadHorsie there's more than one reason why php might not display error messages, my comment is meant to replace the line `ini_set('display_errors', 1);` (or extrapolated into anything that uses ini_set). – Ryan Taylor Feb 27 '17 at 19:22
  • I really don't understand this. According to php_info(), I have error_reporting on E_ALL, and I have display_error on On, and display_startup_errors on On. Still I never see errors on the web page. I feel like I'm in some alternative universe, where this works for everyone all the time, but never, ever, ever, ever for me, no matter which system I'm using and no matter where I put these settings. In the PHP file, the php.ini, the Apache config files, it doesn't matter. Is really nobody else in the known Universe always having this problem except me? – Teekin Apr 08 '17 at 13:23
  • You shloud use string in `ini_set` function as `value` parameter, because your version is incompatible with `strtict_types` mode. Use this instead: `ini_set('display_errors', '1');` – Jakub Bouček Mar 18 '19 at 08:18
  • If you make a loader php file with the code above and then include/require the error suspected php script, it actually *does* catch parse errors. Idk how this was still not mentioned. – Chris S. Dec 21 '21 at 23:01
  • https://www.php.net/manual/en/errorfunc.configuration.php Note: PHP Constants outside of PHP Using PHP Constants outside of PHP, like in httpd.conf, will have no useful meaning so in such cases the int values are required. And since error levels will be added over time, the maximum value (for E_ALL) will likely change. So in place of E_ALL consider using a larger value to cover all bit fields from now and well into the future, a numeric value like 2147483647 (includes all errors, not just E_ALL). – webcoder.co.uk Jan 08 '22 at 22:04
  • So, in my apache virtual DEVELOPMENT host under ErrorLog and CustomLog definitions: `php_flag log_errors on` SPACE `php_flag display_startup_errors on` SPACE `php_flag display_errors on` SPACE `php_value error_reporting 2147483647` SPACE `php_value error_log /var/www/mywebsite.ext/logs/php.error.log` – webcoder.co.uk Jan 08 '22 at 22:05
162

You can't catch parse errors in the same file where error output is enabled 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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Michael Madsen
  • 54,231
  • 8
  • 72
  • 83
159

Inside your php.ini:

display_errors = on

Then restart your web server.

j0k
  • 22,600
  • 28
  • 79
  • 90
user1803477
  • 1,625
  • 1
  • 10
  • 4
  • 8
    +①. On my ubuntu /etc/php5/apache2/php.ini – m93a Feb 23 '15 at 17:44
  • 7
    for *restart* (Debian, Ubuntu, etc.) `sudo service apache2 restart` – Peter Krauss Sep 01 '15 at 13:04
  • 4
    For restart on OS X `sudo apachectl -k restart`. – Pea Jan 10 '16 at 19:53
  • 5
    fun fact: you can locate your php.ini file loaded if you simply put in phpinfo(); into a blank php file. it's the 7th row down and called `Loaded Configuration File` – Frankenmint Jul 24 '16 at 04:56
  • This doesn't work for me. In fact I can call `ini_get('display_errors')` and it returns an empty string (meaning it is turned off). And yes I checked to make sure it is the only line in the configuration file. The setting is getting overridden somehow and I can't figure out why and it is driving me nuts. Yes, I've searched everything in `/etc/php.d/` and it is not one of those files either. Yes, I restarted the web server too. No, there is nothing in the `.htaccess` file. I'm using PHP 7.4.6. – cazort Aug 03 '21 at 18:07
111

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
andre
  • 1,861
  • 1
  • 16
  • 8
  • All I get when doing php -l phpfilename.php is a message that says "Errors parsing sql_lab.php". It doesn't tell me what the errors are. – KevinHJ Jul 23 '21 at 02:33
  • In addition to the blank screen it will return an Error 500 which is way more safe to measure than just no content. – Chris S. Dec 21 '21 at 23:04
54

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kalhua
  • 559
  • 4
  • 2
  • And if you are in nginx environment then add the php value to your site (sites-available) configuration under the location ~\.php directive. fastcgi_param PHP_VALUE " error_reporting=E_ALL;\n display_errors=1;"; – Lazaros Kosmidis Oct 09 '18 at 07:25
45

Warning: the below answer is factually incorrect. Nothing has been changed in error handling, uncaught exceptions are displayed just like other errors. Suggested approach must be used with caution, because it outputs errors unconditionally, despite the display_error setting and may pose a threat by revealing the sensitive information to an outsider on a live site.

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'];
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Frank Forte
  • 2,031
  • 20
  • 19
  • 1
    Do you mean PHP7 or PHP7.1 ? I am confused, I tried as the validated answer proposed and it works, I think you are proposing something a bit different IMHO, indeed "no backward compatibility" and if you have to modify a full PHP < 7 code and need to add `try{} catch() {}` code everywhere in your already defined php code, I don't even want to think the mess that's going to be.. – vdegenne Jan 22 '17 at 11:22
  • @FancyJohn, this could help: `$bt = debug_backtrace(); print_r($bt);`. – Frank Forte Mar 23 '17 at 03:25
  • @ballangddang, I ran into the issue with PHP 7.0, where the only way I could get the error to display was using the try/catch blocks and specifically catching `Error`. If you rewrite all requests (except maybe JavaScript, CSS, Images, etc) to the index.php file, then have the try catch block there, it makes it easier. Yes, any system that does not have a single entry point would be a major headache to update. – Frank Forte Mar 23 '17 at 03:28
  • 1
    Does PHP not show unhandled exceptions? Pretty sure it does? – Martin Tournoij Jun 05 '17 at 02:20
  • It should show unhandled exceptions. If you turn on an output buffer at the start (so that you can send headers at any point before finally flushing the response body) maybe the exception message can get lost somewhere. – Frank Forte Aug 19 '17 at 03:04
  • 1
    This looks like a bad idea. Time to use (or write) a global exception handler. – Paul Spiegel Feb 05 '19 at 14:54
  • @PaulSpiegel, which part is s bad idea? Try/catch blocks? Output buffering mentioned in my previous comment? Each serves a purpose. For example, the try/catch allows you to take a different course if something goes wrong, without adding a big application inside the exception handler. The output buffering allows for server side caching. – Frank Forte Feb 07 '19 at 13:11
  • I mean try/catch block to show exceptions. Why don't you just use [`set_exception_handler`](http://php.net/manual/en/function.set-exception-handler.php)? This way you could separate it from your code. – Paul Spiegel Feb 07 '19 at 13:21
  • So you can handle different exceptions in different ways, depending how critical the exception or error is in your program. – Frank Forte Feb 08 '19 at 19:05
35

This will work:

<?php
     error_reporting(E_ALL);
     ini_set('display_errors', 1);    
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahendra Jella
  • 5,450
  • 1
  • 33
  • 38
  • October 2020: This worked for me. So easy. No messing around with .htaccess or php.ini. When you're done, just comment it out or remove it. PHP 7.4 – Rob Moll Oct 23 '20 at 14:18
35

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43
23

Set this in your index.php file:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sumit Gupta
  • 567
  • 4
  • 12
20

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NavyaKumar
  • 587
  • 5
  • 3
19

In order to display a parse error, instead of setting display_errors in php.ini you can use a trick: use include.

Here are three pieces of code:

File: tst1.php

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

When running this file directly, it will show nothing, given display_errors is set to 0 in php.ini.

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.php 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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Peter
  • 1,247
  • 19
  • 33
  • 1
    I've spent some time figuring this out. Why did they change php 5 behaviour? – Hebe Dec 17 '20 at 20:41
  • 1
    This deserves more upvotes. The key in my case was to enable error reporting and defining an error handler in `init0.php`, and from that file include `init1.php`, which can include other files with errors in them. It won't work if enabling error reporting _in the same file that includes other files with errors in them_. – Magnus Dec 29 '22 at 20:21
  • 1
    The approach suggested is actually correct, but the explanation was wrong. Nothing has been changed in PHP7. Neither PHP5 or PHP3 was able to parse a file with a syntax error. The problem is a parse error, not a PHP version. And the solution proposed is good for any PHP version. – Your Common Sense Feb 04 '23 at 08:29
  • I had a very big web site running PHP5 and using the php.ini setting, all errors were visible, which explain all other answers. When I updated to PHP7, without any other changes, it was not possible to see errors. So even the solution is good for every version, the other answers are correct for older version. – Peter Feb 04 '23 at 13:09
16

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 {
    ...
}
jxmallett
  • 4,087
  • 1
  • 28
  • 35
  • 1
    No, the error reporting is not a loglevel, it is a bitfield. Using 999999 is a **very bad** idea, use some power-of-two minus 1, for example 2047! – peterh Jul 17 '18 at 13:32
  • You're absolutely right, @peterh! I've changed it to E_ALL as this will enable reporting of all errors (except strict errors in php 5.4 and below). – jxmallett Jul 18 '18 at 02:52
16

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);
            break;

        case 'PRODUCTION':
            $base_url = 'Production URL'; /* https://google.com */
            error_reporting(E_ALL);
            ini_set('display_errors', 0);
            ini_set('display_startup_errors', 0);
            ini_set('log_errors', 1); // Mechanism to log errors
            break;

        default:
            exit('The application environment is not set correctly.');
    }
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Channaveer Hakari
  • 2,769
  • 3
  • 34
  • 45
14

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.";
}
chiborg
  • 26,978
  • 14
  • 97
  • 115
  • `find . -name '*.php' -type f -exec php -l {} \; | grep -v 'No syntax errors detected'` is simpler – scones Nov 24 '17 at 13:27
13

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Binit Ghetiya
  • 1,919
  • 2
  • 21
  • 31
12

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");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lintabá
  • 733
  • 9
  • 18
11

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
webcoder.co.uk
  • 341
  • 3
  • 5
7

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joel Wembo
  • 814
  • 6
  • 10
5

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";
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Xakiru
  • 2,556
  • 1
  • 15
  • 11
  • For php7, `catch (Throwable $e)` is better... Or another catch block underneath `catch(Error $e)` – Frank Forte Aug 19 '17 at 02:58
  • This code is actually dangerous. UNDER NO CIRCUMSTANCES it should be used in the production environment. As it will leak the error message to anyone, not only the dev who added it . In the production environment all errors must be **logged.** So the developer can peek into the error log any time. An it will catch all errors, not only ones happened when the dev is refreshing the browser. Without the need of adding any code. Which makes this code not only dangerous but also useless. – Your Common Sense Feb 04 '23 at 09:04
  • Bro, he is quick debugging not launching a website for error catching, being dangerous is irrelevant, nobody would prefer errors to show to clients instead of a blank page. And yes, we could write him a logger or check out what is he using to provide him with the right solution, but this is stackoverflow, it would take a lot of chat and interactions to know his specific needs. your common sense. – Xakiru Feb 05 '23 at 07:45
4
    <?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.

Lahiru Mirihagoda
  • 1,113
  • 1
  • 16
  • 30
pardeep
  • 359
  • 1
  • 5
  • 7
3

Just write:

error_reporting(-1);
jewelhuq
  • 1,210
  • 15
  • 19
3

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().

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Peter Haberkorn
  • 259
  • 1
  • 9
2

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gvlasov
  • 18,638
  • 21
  • 74
  • 110
1

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
NVRM
  • 11,480
  • 1
  • 88
  • 87
1

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);
0

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.

Really Nice Code
  • 1,144
  • 1
  • 13
  • 22
0

As it is not clear what OS you are on these are my 2 Windows cents. If you are using XAMPP you need to manually create the logs folder under C:\xampp\php. Not your fault, ApacheFriends ommitted this.

To read and follow this file do.

Get-Content c:\xampp\php\logs\php_error_log -Wait

To do this in VSCode create a task in .vscode\tasks.json

{ 
  // See https://go.microsoft.com/fwlink/?LinkId=733558 
  // for the documentation about the tasks.json format 
  "version": "2.0.0", 
  "tasks": [ 
    { 
      "label": "Monitor php errors", 
      "type": "shell", 
      "command": "Get-Content -Wait c:\\xampp\\php\\logs\\php_error_log", 
      "runOptions": { 
        "runOn": "folderOpen" 
      } 
    } 
  ] 

and have it run on folder load.

theking2
  • 2,174
  • 1
  • 27
  • 36