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

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.

Related

PHP showing errors despite php.ini forbid it

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.

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 #

How to disable strict PHP parser?

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.

PHP error reporting: some errors to screen, all to log

I want to know how I can display E_ERROR error messages to the screen but write E_ALL error messages to the error log, we currently use the error_reporting() in our app index page so we can change error reporting without the need to constantly restart the web server, but it seems that this (or perhaps the way it's meant to work) means that we only log errors that we see on the screen.
Is there a way to log and display different levels of errors?
Cheers!
You could make a custom error handler, and in your error handler check if the error is an E_ERROR; if so, print it out. Then log the error, regardless of whether it is an E_ERROR or not.
If you're not familiar with custom error handling, the PHP manual has a good example on how to use an error handler to do different things depending on the nature of a PHP error.

Categories