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 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了