Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable error reporting in PHP? [duplicate]

Quite often I will try and run a PHP script and just get a blank screen back. No error message; just an empty screen. The cause might have been a simple syntax error (wrong bracket, missing semicolon), or a failed function call, or something else entirely.

It is very difficult to figure out what went wrong. I end up commenting out code, entering "echo" statements everywhere, etc. trying to narrow down the problem. But there surely must be a better way, right?

Is there a way to get PHP to produce a useful error message, like Java does?

like image 268
Candidasa Avatar asked May 10 '09 09:05

Candidasa


People also ask

How do I enable PHP error reporting?

Quickly Show All PHP Errors The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

How do I show PHP errors on the same page?

= 'Please enter your home page. <br/>'; } if ($_POST['message'] != "") { $_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING); if ($_POST['message'] == "") { $errors . = 'Please enter a message to send.

How do I turn off PHP error reporting?

To turn off or disable error reporting in PHP, set the value to zero. For example, use the code snippet: <? php error_reporting(0); ?>


2 Answers

For syntax errors, you need to enable error display in the php.ini. By default these are turned off because you don't want a "customer" seeing the error messages. Check this page in the PHP documentation for information on the 2 directives: error_reporting and display_errors. display_errors is probably the one you want to change. If you can't modify the php.ini, you can also add the following lines to an .htaccess file:

php_flag  display_errors        on php_value error_reporting       2039 

You may want to consider using the value of E_ALL (as mentioned by Gumbo) for your version of PHP for error_reporting to get all of the errors. more info

3 other items: (1) You can check the error log file as it will have all of the errors (unless logging has been disabled). (2) Adding the following 2 lines will help you debug errors that are not syntax errors:

error_reporting(-1); ini_set('display_errors', 'On'); 

(3) Another option is to use an editor that checks for errors when you type, such as PhpEd. PhpEd also comes with a debugger which can provide more detailed information. (The PhpEd debugger is very similar to xdebug and integrates directly into the editor so you use 1 program to do everything.)

Cartman's link is also very good: http://www.ibm.com/developerworks/library/os-debug/

like image 80
Darryl Hein Avatar answered Oct 06 '22 23:10

Darryl Hein


The following enables all errors:

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

Also see the following links

  • http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors
  • http://php.net/manual/en/errorfunc.configuration.php#ini.display-startup-errors
  • http://php.net/manual/en/function.error-reporting.php
like image 36
Eljakim Avatar answered Oct 06 '22 23:10

Eljakim