PHP doesn't display any error - php

I don't get any PHP error, just get a white page on Firefox, and
Server error
The website encountered an error while retrieving http://example.com/pruebas/prov.php. It may be down for maintenance or configured incorrectly
on Chrome.
This is the code:
if (!ini_get('display_errors')) {
ini_set('display_errors', 1);
}
echo "hola"
echo "hola2";
I intentionally made mistake in the echo "hola" (there's no ';').
I also tried adding to the end of my .htaccess file -> suPHP_ConfigPath /home/username/public_html replacing username with my current username, and then I created a php.ini in public_html with "display_errors = on;". But I'm still not able to get any PHP error.

Your script is dying due to the syntax error before it ever executes, so the ini_set() call is never executed and never takes effect. You'd have to change the setting in the appropriate php.ini.
The actual error message may be in a log file somewhere. Try Apache's error_log, or see if PHP's logging somewhere else.

Make sure that you also have the appropriate error_reporting ini value set as well. You can find more information on PHP.net
Set the error reporting level. The parameter is either an integer representing a bit field, or named constants. The error_reporting levels and constants are described in Predefined Constants, and in php.ini. To set at runtime, use the error_reporting() function. See also the display_errors directive.
In PHP 4 and PHP 5 the default value is E_ALL & ~E_NOTICE. This setting does not show E_NOTICE level errors. You may want to show them during development.
Source

Make sure the display_errors is set to on on your php.ini file.

Related

PHP warning not showing when it should

Update
Setting output_buffering to 0 in php.ini solves this problem. But is there a way to do it without relying on this directive? According to the PHP docs this directive has mode PHP_INI_PERDIR, which means it can't be set with ini_set(), and must be set in one of php.ini, .htaccess, httpd.conf or .user.ini.
Original question
I know how to solve the "Cannot modify header information" PHP warning, but I can't seem to get this warning to appear, even when I try to modify the headers after output. For example:
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo ini_get('error_reporting');
header('location:./');
Will not produce an error and will just redirect like nothing happened. This behavior doesn't happen in my testing environment (I will get the error as expected). These are my setups for dev vs testing:
Development
PHP v5.5.15
error_reporting = E_ALL
display_errors = On
Testing
PHP v5.3.27
error_reporting = E_ALL
display_errors = On
I suspect there might be another PHP setting for this but I haven't been able to find it. Any ideas?
The PHP directive output_buffering must be set to 0 in order to see output before sending a redirect header. This can only be accomplished by:
Setting the directive in php.ini or .user.ini
output_buffering = 0
Or when using Apache and modifying .htaccess or httpd.conf
php_flag "output_buffering" Off
With output_buffering on, any echos or PHP notices and warnings will be stored in a variable and won't be output until the script is done running. Setting a location header will not generate a PHP warning because because with output buffering, headers won't be sent as soon as there is output. The script reaches the end, and the entire blob of output including the location header is sent at once and the page is allowed to redirect normally.
I wanted to be able to see PHP warnings and notices that were previously going undetected because output was being saved until the end and the redirect hid the errors. It's probably a good idea to turn output_buffering off for your development environment so that you are not unaware of these errors. For production, output_buffering may have performance improvements, and error_reporting should be turned off anyway so you wouldn't be missing anything.
Note: You can also make a call to flush() or ob_end_flush() before the call to header() and it will produce the desired warning.

What's the purpose of ini_set() in php? (especially for error reporting)

Ok so PHP has the function ini_set() which a lot of people are aware of and will use to set various configuration options (here) to help with development etc. However, this function does only seem to work at runtime and will not work if there are any fatal errors or the script has syntax errors and can't be parsed / compiled.
Therefore surely there is no point of doing this (from the manual):
http://php.net/manual/en/function.ini-set.php
Examples
Example #1 Setting an ini option
<?php
echo ini_get('display_errors');
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
echo ini_get('display_errors');
?>
I don't know if I'm just missing something and my php.ini isn't configured correctly, but a lot of the time I get no errors. For beginners / juniors there will no doubt be a lot of syntax errors (missing semi-colons, closing brackets etc), and said juniors would search for how to turn on errors, assume the above manual entry is correct, yet when re-running their script, alas, they get no errors as the script cannot be parsed / compiled in the first place.
I know you can set display_errors = On in the php.ini file and restart your web server to show all errors to the screen (using this in a development environment, definitely not live), but wouldn't it be better just to remove the function and only configure the php.ini file for different error levels?
Update:
I know ini_set isn't just for displaying errors, but code can't be very manageable if you're calling ini_set in certain scripts / functions / files and wouldn't it make more sense to use the php.ini for something like that?
Update
So the ini file can be used to set global configuration options, surely you'd use this for security or optimisation, however developers could still use ini_set to override some of these options at runtime which may not be desirable
In summary (#Hanky웃Panky):
Why do I have the option of displaying errors when some trivial syntax errors will still not display?
yes, you are right that its better just to remove the function and only configure the php.ini file for different error levels.
But, this is good only that case when you have only one project in your machine, So, its all configuration setting you can do in php.ini
Consider, the case if you have multiple project setup. if you don't want some settings in that project still it will get from php.ini
So, it is suggested for some configuration settings you just set them at project level with ini_set() and will not reflect other projects.
string ini_set ( string $varname , string $newvalue );
The Purpose of ini_set is to set the value of the given configuration option.
This newvalue is kept by the configuration option during the script execution and restored at the scripts ending.
Example for setting an ini option
<?php
echo ini_get('display_errors');
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
echo ini_get('display_errors');
?>
string ini_set ( string $varname , string $newvalue )
Basically ini_set() sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending.
for all the variables which you can configure during the script run. please go through the below link.
Another settings can be configured at runtime using the the ini_set() function:
memory_limit and max_execution_time
(From ZCE test part about PHP Basics).
ini_set — Sets the value of a configuration option. Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution, and will be restored at the script's ending, without ini_set(), values from php.ini file will be used.
EDIT:
You may find this helpful:
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

I can't disable PHP errors

So what's going on is I tried
ini_set('display_errors', 'Off');
error_reporting(0);
Right below <?php, but this didn't seem to stop displaying them. So I went to the php.ini and went to display_errors and saw that it was set to Off. But it still showed.
So I went and did phpinfo() and display_errors along with display_startup_errors are both off. Also html_errors is off. I'm not sure if this will help, but it says error_reporting is set to -10241. Any ideas?
Do not change the value of error reporting to solve the issue. If display_errors is off, errors are not display independently of the error_reporting setting. This way you will not display errors but you can still log them.
The following should work:
ini_set('display_errors', 'Off');
If it doesn't work it could be that your server configuration does not allow you to change settings from PHP scripts. ini_set() returns FALSE on failure. So first of all you should check what value that call is returning. Make sure that ini_set is not listed among disabled PHP functions (disable_functions in php.ini).
If you are asking yourself why errors are still being displayed even if in php.ini the display_errors is Off, you can check the actual value of display_errors during the script execution:
ini_get('display_errors')
Pemember that PHP settings could be changed also in Apache host configuration and in .htaccess files. So check if you have an htacces that enables display_errors. Something like this:
php_flag display_errors on
Try to use:
ini_set('display_errors', 0);
ini_set('display_errors', false);
You don't describe what the errors are, so it's possible that your web server (Apache, nginx, etc) are what's throwing the error and not PHP.
If it is PHP, ensure that you're editing the correct php.ini as identified in your phpinfo.php. Remember that if you edit the php.ini, you will need to restart your PHP process (for example, on some *nix systems: service php-fpm restart. Your exact command may vary.)
If it's off in your php.ini, my guess is that it's being overridden somewhere else -- either later in the script ('grep "ini_set" /path/to/project/*.php' will find it). Also, the PHP Manual states that if the script has fatal errors, it doesn't apply if there are fatal errors:
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.

Controlling PHP's output stream

I want to keep errors out of my PHP output stream. I only want output of things I explicitly echo.
Looking at my php.ini, is "display_errors" the only configuration I need to change?
Instead of modifying php.ini, you can call this at a very early part of your code:
error_reporting(0);
Note that this means fatal errors will die silently as well, so it makes it a little difficult to debug at first.
I only recommend that if we're talking about a production machine. display_errors will hide them from the user, but make sure you have log_errors and error_log set in the php.ini so you'll see them on your regular log analysis (you do, right?).
For a development machine, I recommend keeping display_errors on and error_reporting(E_ALL | E_STRICT) so you'll see if anything is fishy.
You can modify that INI directive and change the flag to 0 (False) or disable error_reporting on a page by page basis
error_reporting(0)
Typically Production environments should be on display_errors = 0 Though not all of us have both Development and Production environments
You can also change the "verbosity" of the error messages by passing different values to error_reporting function (Or by changing the INI value for it in php.ini) More information on that can be found here: PHP: Runetime Configuration - error_reporting

Categories