PHP showing errors despite php.ini forbid it - php

we have forbidden to show errors on our server. But I'd need to show the errors in my script despite it.
I tried this:
<?php
error_reporting(E_ALL);
ini_set("display_errors", "on");
echo "chyba"
echo "nazdárek";
?>
But it is not useful. Thank you for your help.

Your call to error_reporting() doesn't do anything because it does not run.
There is a missing ; after the first echo. I know you know about it, you made the mistake on purpose, to show that error_reporting() doesn't do what you expect it to do.
It doesn't work this way. The missing semicolumn is a syntax error. The script does not compile, so it does not run. Your call to error_reporting() is not executed and that means the value of the error_reporting configuration directive is the one that decides what errors are reported.
You have to fix the syntax errors first, make the script compile & run, and only after that try to trigger a runtime error and see if it is reported back to you. I bet it is.
A runtime error or warning is easy to generate. Try a division by zero, for example.

What you're trying to produce there is a syntax error. This won't work within the same file that you're setting error reporting. The file first needs to be parsed in its entirety. If there's a syntax error in the file, then none of its code will be executed, so no error reporting will be switched on.

Related

Why doesn't PHP show all of the syntax errors?

This is my error setting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL|E_PARSE);
Now I write this test code:
helloooooo;
And It shows me:
Use of undefined constant helloooooo ...
So far so good.
Now I remove the semicolon from end of my so-called code:
helloooooo
Shows nothing. It shows a white page.
In my experience whenever I see a white page there is a syntax error so I look for a typo.
The question is why doesn't PHP help in this case?
Someone said :
Syntax checking is done before executing any code. So it can't execute
the ini_set() functions if there's a syntax error.
How about PHP interpreter applies settings first then executes the rest of code?
JavaScript interpreter can detect the same error in runtime.
Just try this:
<script>
helloooooo
</script>
Now go to Firefox => Tools=> WebDeveloper => WebConsole
ReferenceError: helloooooo is not defined
PHP's default error reporting mechanism (might) suppress error output. If you're trying to turn it on at runtime using the error_reporting function, then PHP will first have to successfully parse the file, then execute it to set that new desired error reporting level. That fails when there are syntax errors, because then PHP can't successfully parse and execute the file to change the error reporting level. The syntax error is getting suppressed because… well… it couldn't change the error reporting level yet.
Javascript doesn't have that, it outputs any and all errors it encounters.
The reason PHP likes to suppress errors is because its running on the server, and internal server debug details shouldn't be shown on public web pages. In Javascript that is moot, since it's running in the browser anyway, and the errors are reported to the console, which regular users don't see. Moreover, PHP does log all errors to a log file, so the same way you go looking for errors in Javascript, you could explicitly go looking for them in PHP's log file as well.

How to change a setting to true in a PHP file?

I have an Apache2 web server with PHP 5.5 installed.
My default PHP settings is display_error = 0 (I don't need globally displayed errors) but I need it on in specific PHP files.
I tried with:
error_reporting(E_ALL);
ini_set("display_errors", 1);
and it's not working.
Can someone tell me how can I make it show errors in specific PHP files?
i am try to force some error writing some no syntax logic and not showing error...
To show parse errors in PHP you have to put this on your php.ini
display_errors = on
My advice is to avoid displaying errors on production servers but log them all. So you can later inspect and fix bugs from yoursite-error.log file.
You should be concerned if your applications has warnings, errors etc. IMHO it is a bad idea to focus your attention only on few files instead of them all.
Although display_errors may be set at runtime (with ini_set()), it won't have any effect if the script has fatal errors. This is because the desired runtime action does not get executed.

PHP error_reporting issue, even local = On

in my php.ini the display_errors setting is set to Off by standard. Usually I started my index.php file with
ini_set('display_errors', 'On');
error_reporting(E_ALL & ~E_NOTICE);
which all worked fine for my needs.
Recently, the error reporting did not work anymore. notices have been thrown out though, so:
<?php echo $i ?>
throws a notice "undefined constant"
<?php badbadbad ?>
simply returns a blank page instead of a Fatal Error message.
Checking phpinfo() shows that the Master value for display_errors is Off, while the local value is set to On (as expected). Nevertheless I could not get any error message.
Changing my php.ini setting for display_errors = On gets me around this issue, but should not be a solution for ever.
Any hints?
Thanks & brgds
David
I think the reason your second example fails is that it's actually a Parse Error.
When executing code, PHP has to first parse and compile the whole file it's going to execute and then only after that it can run it. If there's a problem during parsing, even if the problem is at the end of the file, none of the file's code will be executed -- so neither will your ini_set() and error_reporting() statements.
If the erroneous code is in the same file as said statements, there's not much you can do. If it's in another file that is used with include or require, make sure that the including calls are made after setting the new error reporting values. At least that works for me with PHP >= 5.4.

php error reporting for single page?

Is it possible (and if so, how) to turn on php error reporting to the screen for just one page? I currently have all error reporting to the screen switched off in php ini.
Or another way of asking - if I use error_reporting(E_ALL) on a single page, will it also display errors on other pages (what I don't want)?
Thanks!
Using error_reporting(E_ALL) will enable error reporting for only that page!
To display errors use, ini_set("display_errors", 1)
If you call error_reporting() within a PHP script, it will only affect that script's runtime (i.e. anything it executes after that call, including other scripts which have been included). It won't modify server configuration etc.
However, it's worth noting that if the script encounters an error before it can make that call, it won't have any effect (i.e. error reporting behaviour will be unchanged). That can be a particular problem if you have a syntax error or similar.
EDIT: It occurs to me that you may want to leave error reporting enabled in your configuration, but only enable display_errors as needed. That way you can still get errors written to a log file, but they'll only appear on-screen when you want.
Call error_reporting(0) at the beginning of a script to do this. It will block all runtime errors. However, it isn't recommended to do this, except possibly in production code.
Read more about error_reporting() here

What to do when no errors and error-reporting enabled?

Im lost here, closed few files and clicked yes on save all and now page wont load and error_reporting(1); its in all documents... No erors given
If your error isn't a syntax error, you can manually debug by placing die('test'); in various areas of your code to see if that portion executes.
You also need to turn on the display of errors, so at the top of your file add:
error_reporting(E_ALL);
ini_set("display_errors", 1);
Turn on error_reporting and display_errors in your php.ini file. If you have a syntax errror in the script, most likely the script's being killed long before execution could ever reach in the in-script ini_set/error_reporting overrides.
You might want to check the server's error log, as fatal errors generally get sent there if error reporting/display is turned off.

Categories