[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

How do I get PHP errors to display?

  • This topic is empty.
Viewing 13 posts - 16 through 28 (of 28 total)
  • Author
    Posts
  • #10165
    xakiru
    Participant

    The best/easy/fast solution that you can use if it’s a quick debugging, is to surround your code with catching exceptions. That’s what I’m doing when I want to check something fast in production.

    try {
        // Page code
    }
    catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }
    
    #10166
    joel-wembo
    Participant

    This code on top should work:

    error_reporting(E_ALL);
    

    However, try to edit the code on the phone in the file:

    error_reporting =on
    
    #10174
    peter
    Participant

    In order to display a parse error, instead of setting display_errors in php.ini you can use a trick: use include.

    Here are three pieces of code:

    File: tst1.php

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    // Missing " and ;
    echo "Testing
    

    When running this file directly, it will show nothing, given display_errors is set to 0 in php.ini.

    Now, try this:

    File: tst2.php

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    include ("tst3.php");
    

    File: tst3.php

    <?php
    // Missing " and ;
    echo "Testing
    

    Now run tst2.php which sets the error reporting, and then include tst3. You will see:

    Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in tst3.php on line 4

    #10167
    lintab
    Participant

    You can add your own custom error handler, which can provide extra debug information. Furthermore, you can set it up to send you the information via email.

    function ERR_HANDLER($errno, $errstr, $errfile, $errline){
        $msg = "<b>Something bad happened.</b> [$errno] $errstr <br><br>
        <b>File:</b> $errfile <br>
        <b>Line:</b> $errline <br>
        <pre>".json_encode(debug_backtrace(), JSON_PRETTY_PRINT)."</pre> <br>";
    
        echo $msg;
    
        return false;
    }
    
    function EXC_HANDLER($exception){
        ERR_HANDLER(0, $exception->getMessage(), $exception->getFile(), $exception->getLine());
    }
    
    function shutDownFunction() {
        $error = error_get_last();
        if ($error["type"] == 1) {
            ERR_HANDLER($error["type"], $error["message"], $error["file"], $error["line"]);
        }
    }
    
    set_error_handler ("ERR_HANDLER", E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
    register_shutdown_function("shutdownFunction");
    set_exception_handler("EXC_HANDLER");
    
    #10175
    sumit-gupta
    Participant

    Set this in your index.php file:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    
    #10163
    peter-haberkorn
    Participant

    If you have Xdebug installed you can override every setting by setting:

    xdebug.force_display_errors = 1;
    xdebug.force_error_reporting = -1;
    

    force_display_errors

    Type: int, Default value: 0, Introduced in Xdebug >= 2.3 If this
    setting is set to 1 then errors will always be displayed, no matter
    what the setting of PHP’s display_errors is.

    force_error_reporting

    Type: int, Default value: 0, Introduced in Xdebug >= 2.3
    This setting is a bitmask, like error_reporting. This bitmask will be logically ORed with the bitmask represented by error_reporting to dermine which errors should be displayed. This setting can only be made in php.ini and allows you to force certain errors from being shown no matter what an application does with ini_set().

    #10164
    pardeep
    Participant
        <?php
        // Turn off error reporting
        error_reporting(0);
    
        // Report runtime errors
        error_reporting(E_ERROR | E_WARNING | E_PARSE);
    
        // Report all errors
        error_reporting(E_ALL);
    
        // Same as error_reporting(E_ALL);
        ini_set("error_reporting", E_ALL);
    
        // Report all errors except E_NOTICE
        error_reporting(E_ALL & ~E_NOTICE);
        ?>
    

    While your site is live, the php.ini file should have display_errors disabled for security reasons. However, for the development environment, display_errors can be enabled for troubleshooting.

    #10160
    gvlasov
    Participant

    If it is on the command line, you can run php with -ddisplay_errors=1 to override the setting in php.ini:

    php -ddisplay_errors=1 script.php
    
    #10161
    nvrm
    Participant

    In Unix CLI, it’s very practical to redirect only errors to a file:

    ./script 2> errors.log
    

    From your script, either use var_dump() or equivalent as usual (both STDOUT and STDERR will receive the output), but to write only in the log file:

    fwrite(STDERR, "Debug infos\n"); // Write in errors.log^
    

    Then from another shell, for live changes:

    tail -f errors.log
    

    or simply

    watch cat errors.log
    
    #10159
    shaikh-nadeem
    Participant

    Report all errors except E_NOTICE

    error_reporting(E_ALL & ~E_NOTICE);
    

    Display all PHP errors

    error_reporting(E_ALL);  or ini_set('error_reporting', E_ALL);
    

    Turn off all error reporting

    error_reporting(0);
    
    #10168
    webcoder.co.uk
    Participant

    Accepted asnwer including extra options. In PHP files for in my DEVELOPMENT apache vhost (.htaccess if you can ensure it doesn’t get into production):

    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);
    

    However, this doesn’t make PHP to show parse errors – the only way to show those errors is to modify your php.ini with this line:

    display_errors = on
    

    (if you don’t have access to php.ini, then putting this line in .htaccess might work too):

    // I've added some extra options that set E_ALL as per https://www.php.net/manual/en/errorfunc.configuration.php.
    php_flag log_errors on
    php_flag display_errors on
    php_flag display_startup_errors on
    php_value error_reporting 2147483647
    php_value error_log /var/www/mywebsite.ext/logs/php.error.log
    
    #10157
    really-nice-code
    Participant

    If you are on a SharedHosting plan (like on hostgator)… simply adding

    php_flag display_errors 1
    

    into a .htaccess file and uploading it to the remote folder may not yield the actual warnings/errors that were generated on the server.

    What you will also need to do is edit the php.ini

    This is how you do it via cPanel (tested on hostgator shared hosting
    plan)

    After logging into your cPanel, search for MultiPHP INI Editor.
    It is usually found under the SOFTWARE section in your cPanel list of items.

    On the MultiPHP INI Editor page …you can stay on the basic mode tab and just check the button on the line that says display_errors.
    Then click the Apply button to save.

    enter image description here

    IMPORTANT: Just remember to turn it back off when you are done debugging; because this is not recommended for public servers.

    #10158
    theking2
    Participant

    As it is not clear what OS you are on these are my 2 Windows cents.
    If you are using XAMPP you need to manually create the logs folder under C:\xampp\php. Not your fault, ApacheFriends ommitted this.

    To read and follow this file do.

    Get-Content c:\xampp\php\logs\php_error_log -Wait
    

    To do this in VSCode create a task in .vscode\tasks.json

    { 
      // See https://go.microsoft.com/fwlink/?LinkId=733558 
      // for the documentation about the tasks.json format 
      "version": "2.0.0", 
      "tasks": [ 
        { 
          "label": "Monitor php errors", 
          "type": "shell", 
          "command": "Get-Content -Wait c:\\xampp\\php\\logs\\php_error_log", 
          "runOptions": { 
            "runOn": "folderOpen" 
          } 
        } 
      ] 
    

    and have it run on folder load.

Viewing 13 posts - 16 through 28 (of 28 total)
  • You must be logged in to reply to this topic.