PHP警告未显示何时应该显示

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.