How to disable strict PHP parser? - php

On my WordPress blog, whenever I have a PHP error, it breaks the site and displays a white page with the PHP error.
For example:
Parse error: syntax error, unexpected ';' in...
I can fix the error. However, I don't want users to see a broken site while I do. Is there a setting I can enable that will suppress the error (or more gracefully handle it) and continue to parse the rest of the PHP?

Sure: it's the setting whereby you don't make a parse error in your script. It's a setting in you.
(Parse errors generally can't be recovered from; what meaning does your script have if the parser cannot understand it? If it's not written in valid PHP? What do you want PHP to do?)

When you have a syntax error in a PHP page, it will always immediately stop parsing. It's the same thing as a compile error in languages such as C#, VB etc. There is no getting around this.
There are two ways you can suppress this error message. The first is with the error_reporting directive in your php.ini file. It's likely set to E_ALL & ~E_DEPRECATED. You can fine tune what is reported (via error logging and out to the display) by configuring the directives located on the PHP Site.
The other (simpler) way to suppress these is via the display_errors directive in your php.ini file. Simply set this to Off and restart your web server and you won't see these errors any longer - just a white page.

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

PHP not reporting specific error (double opening php tag)

I've just had a hard time finding a bug in my PHP code described below
<?php
if(condition...)
{
do something...
}
<?php
...more php
Notice that I incorrectly opened a php block again when it was not needed(before the ...more php)
The problem is that PHP is not reporting this error, it's just giving me a blank page. I've set error reporting to E_ALL and ini_set display errors '1'. I was wondering if there is a way to make php report this kind of error.
This is very common problem, yet a quite simple one.
That's just error reporting settings.
Your server apparently set up to not to display errors (which is the only right behavior on a live server!), but most likely it writing errors in some log file.
You have to find out where this file located and peek for errors.
If it's not a live server, you may turn on display_errors setting in the php.ini
What do you mean? PHP does detect that problem and does report the error, if you have errors turned on:
Parse error: syntax error, unexpected '<' in ### on line #

What are the reasons why PHP would echo errors, even with error_reporting(0)?

What are some reasons why PHP would force errors to show, no matter what you tell it to disable?
I have tried
error_reporting(0);
ini_set('display_errors', 0);
with no luck.
Note the caveat in the manual at http://uk.php.net/error_reporting:
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).
If your underlying system is configured to report E_STRICT errors, these may be output before your code is even considered. Don't forget, error_reporting/ini_set are runtime evaluations, and anything performed in a "before-run" phase will not see their effects.
Based on your comment that your error is...
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /usr/home/REDACTED/public_html/dev.php on line 11
Then the same general concept applies. Your code is never run, as it is syntactically invalid (you forgot a ';'). Therefore, your change of error reporting is never encountered.
Fixing this requires a change of the system level error reporting. For example, on Apache you may be able to place...
php_value error_reporting 0
in a .htaccess file to suppress them all, but this is system configuration dependent.
Pragmatically, don't write files with syntax errors :)
To prevent errors from displaying you can
Write in a .htaccess: php_flag display_errors 0
Split your code in separate modules where the
main (parent) PHP file only sets the
error_logging and then include() the
other files.
Use phpinfo to find the loaded php.ini and edit it to hide errors. It overrides what you put in your script.
Is set_error_handler() used anywhere in your script? This overrides error_reporting(0).
Pragmatically, don't write files with syntax errors :)
To ensure there's no syntax errors in your file, run the following:
php -l YOUR_FILE_HERE.php
This will output something like this:
PHP Parse error: syntax error, unexpected '}' in Connection.class.php on line 31
Just add the below code in your index.php file:
ini_set('display_errors', False);
Use log_errors for them to be logged instead of displayed.
If the setting is specified in Apache using php_admin_value, it can't be changed in .htaccess or at runtime.
See: How to change configuration settings

Categories