3
votes

I am new to PHP and trying to understand how to debug the code. I am using the ini_set and ini_get functions.

Here's my code:

<?php
 error_reporting(E_ALL);

 ini_set('display_errors', 1);
 ini_set('log_errors', 1);
 ini_set('log_errors_max_len', 0); // 0 = unlimited length
 ini_set('error_log', '/var/www/html/debugging/php_errors.log');

 echo $error->abc; // should give error


 // E_ERROR - run out of memory
 ini_set('memory_limit', '1K');
 var_dump((object) range(0, 100000));
 require 'abc.php';

?>

All the errors get logged into the php_errors.log file as well are displayed on the browser. But when I use var_dump((object) range(0, 100000)); to introduce a fatal error to run out of memory, I get a blank screen on the browser, whereas the error gets logged into the php_errors.log.

Do I need to change any other setting to get the error displayed on the browser as well as log into the file. I am just trying to play with PHP here.

1
What mode is PHP running in? apache/mod_php or fpm?Matt
it is running in Apache mode Server API: Apache 2.0 HandlerPritam Bohra

1 Answers

1
votes

1K is too low for PHP to render the error in the browser.

[Updated]

PHP needs to have enough memory available in order to be able to trigger the error handler that displays the error messages in the browser. So in this case, php did not have enough memory when it tried to output the message to the browser and just stopped.

The amount of memory required depends on loaded extensions, server environment, and other php.ini settings.

For example, when an out of memory error is triggered at 1 MB allocated, it requires 1.5 MB in order to display an error message in the browser. See: Tracking Memory Usage in PHP

Demo: http://ideone.com/VmPO0w

<?php
// error.php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$x = str_repeat(' ', 1024 * 1024); //store 1 MB to a string
ini_set('memory_limit', '1535K'); //minimum of 1536K (1.5 MB) needed to display error
while (true) {
    echo 'not real: ' . (memory_get_peak_usage(false) / 1024 / 1024) . " MB\n";
    echo 'real: ' . (memory_get_peak_usage(true) / 1024 / 1024) . " MB\n\n";
    $x .= str_repeat(' ', 1024 * 500); //store 500K more to string
}

Command line usage result: $>php error.php

not real: 1.2208786010742 MB
real: 1.5 MB


Fatal error: Allowed memory size of 1571840 bytes exhausted (tried to allocate 512001 bytes) in error.php on line 10

While when loaded in the browser, the result would be a blank page.


Windows Environment php 5.6 x64 NTS + built-in webserver. Use at least 256K.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo $error->abc; // should give error
// E_ERROR - run out of memory
ini_set('memory_limit', '256K'); //tested with 255K - blank page
var_dump((object) range(0, 100000));

( ! ) Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 32 bytes) in ...


Linux Environment php 5.6 x64 NTS + php-fpm + Apache FCGI. Use at least 512K.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo $error->abc; // should give error
// E_ERROR - run out of memory
ini_set('memory_limit', '512K'); //test with 511K - blank page
var_dump((object) range(0, 100000));

( ! ) Fatal error: Allowed memory size of 524288 bytes exhausted (tried to allocate 32 bytes) in ...