drhozgt6007 2016-12-20 18:52
浏览 95
已采纳

PHP致命错误未在浏览器屏幕上显示

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条回答 默认 最新

  • dongshadu2546 2016-12-20 20:30
    关注

    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
    ";
        echo 'real: ' . (memory_get_peak_usage(true) / 1024 / 1024) . " MB
    
    ";
        $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 ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)