27

When executing a PHP page through browser , we will just get the output but not the errors in code.

how can i view the errors occurred by the code in the backend??

I am using the following in the code for error reporting..

error_reporting(E_ALL | E_ALL);
John Tor
  • 837
  • 2
  • 8
  • 11
  • Related, involves setting error-reporting levels properly: [How do I isolate unwanted PHP error messages?](http://stackoverflow.com/questions/6546690/how-do-i-isolate-unwanted-php-error-messages). Also related, [PHP not displaying errors even though display_errors = On](http://stackoverflow.com/questions/6480425/php-not-displaying-errors-even-though-display-errors-on). –  Jul 05 '11 at 19:09

3 Answers3

62
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • 1
    I didn't downvote, but why do you have a `^`? Isn't that XOR or something? Why do you want to XOR? –  Jul 05 '11 at 19:11
  • @Keoki Zee, display all errors except notice (uninitialized variables etc)... it is the default setting ;) – Dejan Marjanović Jul 05 '11 at 19:14
  • 1
    Okay, that looks ok to me...still don't know why this got downvoted :/ –  Jul 05 '11 at 19:16
3

Try -1. From the documentation, "Passing in the value -1 will show every possible error, even when new levels and constants are added in future PHP versions."

// Report all PHP errors
error_reporting(-1);

If that doesn't work, try to do an ini_set:

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
  • 1
    And better make the change in the php.ini. Changing the error reporting in a script is useless if the script has syntax errors - it'll be killed long before the error_reporting/ini_set calls ever could get executed. – Marc B Jul 05 '11 at 19:03
  • @Marc B, are you sure about that? – Dejan Marjanović Jul 05 '11 at 19:10
  • @Marc B is correct. If your script has syntax errors, nothing executes. – Michael Irigoyen Jul 05 '11 at 19:12
  • @webarto: if there's a syntax error in the script, it's killed during compile phase. If you've got error_reporting turned off in the .ini file, then it's impossible for that script to turn it on, because the directive to turn it on will never get executed. You'd never get the error message. – Marc B Jul 05 '11 at 19:12
  • @Marc B, PHP is scripting language it is not compiled by default, you are wrong, it will display error as long as error_reporting is before that particular error. – Dejan Marjanović Jul 05 '11 at 19:17
  • @webarto You are incorrect. When you point your browser at a PHP file, the server handles that request initially. It tells the PHP processor to compile that file. Once the file is compiled, it delivers it's output buffer to the browser. If a syntax error happens in the compile phase, the entire script dies, negating anything that should run in that script. – Michael Irigoyen Jul 05 '11 at 19:20
  • 1
    @webarto: might want to read up on PHP. There is a compile phase, where the raw PHP text you've written is converted into PHP opcodes. It is these opcodes that the various PHP caches (APC) store. The "conversion" is the compile phase. – Marc B Jul 05 '11 at 19:22
  • @Marc B, you have said, if fatal error is encountered anywhere in the script, it wont display anything? Ok, please run this code... http://pastebin.com/raw.php?i=G0DLdgS6 ... It will display everything until the line with fatal error... If you use ini_set on top of script or on init or whatever it error will be displayed. Example http://firstbeatmedia.info/error.php – Dejan Marjanović Jul 05 '11 at 19:25
  • 2
    @webarto: yes, but your php will have error_reporting turned on at the php.ini level, so the errors will be displayed. I'm saying that if you're on a production system, where error_reporting is turned OFF at the .ini level, there is no way for a script with a syntax error to override that to display the compile-time error. – Marc B Jul 05 '11 at 19:27
  • @Marc B, on a production server, errors should be logged and nice error page shown, not displayed, I maybe misunderstood you answer but you can turn on error reporting from PHP regardless if script has fatal error... screenshot from Kohana index.php: http://pokit.etf.ba/get/05afd7df39aab4a2056c8c7470456600.png, again, if I misunderstood your answer, sorry. – Dejan Marjanović Jul 05 '11 at 19:36
  • @webarto: you're right, errors should be logged. But again, how can `error_reporting(new setting here)` be executed in a script, if the script doesn't even execute due to a fatal error? It's like trying to drive a car out of a factory before the engine's been installed. No matter how you turn the key, there's nothing there to turn on. – Marc B Jul 05 '11 at 19:38
  • @Marc B, you will turn the ignition on, start fuel pump, send high voltage current through cables, but car won't start because there is no engine. I am saying that script will execute (until line with error) even if there is fatal error, http://firstbeatmedia.info/error.php... – Dejan Marjanović Jul 05 '11 at 19:43
  • 1
    @webarto: Exactly. And if the dashboard's been configured to NOT display the "check engine" light (error_reporting = 0 in the .ini file), there is no way for the car to tell you that there's no engine. Again, you're completely misreading the answer. Most servers have error_reporting turned off at the .ini level. it is IMPOSSIBLE for a script with syntax errors to change that setting at run-time, because there will NEVER **BE** a runtime. If error_reporting is on, then the syntax error will be reported, but again, the script cannot change the reporting level, because it WILL NOT EXECUTE. – Marc B Jul 05 '11 at 19:46
  • @Marc B, I am sorry Marc, but my server is configured in that way (error reporting disabled at php.ini)... everything will be executed until the fatal error. Keyword runtime not compile time... Please try the script I have posted on your server. – Dejan Marjanović Jul 05 '11 at 19:51
  • 1
    Never mind. It's pointless to argue further. PHP kills scripts at compile time if there's syntax errors, which makes it impossible for the script to do ANYTHING (including change settings via ini_set). Regardless of what you think your test script does, the reality is you're not proving anything. – Marc B Jul 05 '11 at 19:53
  • 1
    An undefined function call is NOT a compile-time error in PHP, because the PHP compiler has no way of knowing if the function will not be auto-loaded or dynamically created at runtime. So your script DOES execute, but kills itself once it tries to call xxx(). I'm talking about syntax errors that prevent the script from compiling AT ALL. – Marc B Jul 05 '11 at 20:19
  • FYI: `Most of E_STRICT errors are evaluated at the compile time thus such errors are not reported in the file where error_reporting is enhanced to include E_STRICT errors (and vice versa).` - [PHP Manual](http://www.php.net/manual/en/function.error-reporting.php) – JSmyth Jan 19 '14 at 15:55
1

Inside your php.ini, set the display_errors to On:

display_errors = On

Then restart your web server.

amrezzd
  • 1,787
  • 15
  • 38