468

UPDATE 2:

I have now removed the following from the .php file:

<?php error_reporting( E_ALL ); ?>

I have set display_erros in php.ini as follows:

display_errors = On

Error reporting is set to the following in php.ini:

error_reporting = E_ALL | E_STRICT

After restarting Apache, I still get no errors/warnings.

UPDATE 1:

I have changed error_reporting in php.ini from:

error_reporting = E_ALL & ~E_DEPRECATED

to

error_reporting = E_ALL | E_STRICT

After which I restarted Apache, e.g.

/etc/init.d/apache2 restart

But the page will still not display errors/warnings of any kind.

ORIGINAL QUESTION:

The following script is generating a warning because the $err being inside the if statement. Why is this warning not being displayed on the PHP page in a web browser?

I have to look at the Apache logs to see the warning. Also, if I deliberately change the "insert into" to "delete into", it does not display an error on the PHP page. Why are the errors not displaying on the actual PHP page?

<?php
    error_reporting(E_ALL);
?>

<html>
    <head>
        <title></title>
        <link rel="icon" type="image/png" href="favicon.ico">

        <?php
            if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                $err = array();

                if (empty( $_POST['display_name']))
                    $err[] = "display name field is required";
                if (empty( $_POST['email']))
                    $err[] = "email field is required";
                if (empty( $_POST['password']))
                    $err[] = "password field is required";

                if (!$err) {
                    try {
                        $DBH = new PDO("mysql:host=localhost;dbname=database1", "user", "pass");
                        $DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                        $STH = $DBH->prepare("delete into table1 (display_name, email, password) values ( :display_name, :email, :password )");

                        $STH->bindParam(':display_name', $_POST['display_name'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':email', $_POST['email'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':password', $_POST['password'], PDO::PARAM_STR, 100);

                        $STH->execute();

                        $STH = $DBH->prepare("delete into table2 ( username, status, users_id ) values ( :username, :status, :users_id )");

                        $strStatus = 1;

                        $STH->bindParam(':username', $_POST['display_name'], PDO::PARAM_STR, 100);
                        $STH->bindParam(':status', $strStatus, PDO::PARAM_INT, 1);
                        $STH->bindParam(':users_id', $_POST['referer'], PDO::PARAM_INT, 1);

                        $STH->execute();

                        $DBH = null;
                    }
                    catch (PDOException $e) {
                        echo $e->getMessage();
                    }

                    header("Location: " . $_SERVER['PHP_SELF']);
                    exit;
                }
                else {
                    foreach ($_POST as $key => $val) {
                        $form[$key] = htmlspecialchars($val);
                    }
                }
            }
            else {
                $form['display_name'] = $form['email'] = $form['password'] = '';
            }
        ?>
    </head>

    <body>
        <?php foreach($err as $line) { ?>
        <div style="error"><?php echo $line; ?></div>
        <?php } ?>

        <h1>Register</h1>

        <form method="post">
            Referers id:<br/>
            <input type="text" name="referer" /><br/><br/>

            Name:<br/>
            <input type="text" name="display_name" value="<?php echo $form['display_name']; ?>" /><br/><br/>

            Email:<br/>
            <input type="text" name="email" value="<?php echo $form['email']; ?>" /><br/><br/>

            Password:<br/>
            <input type="text" name="password" value="<?php echo $form['password']; ?>" /><br/><br/>

            <input type="submit" value="register" />
        </form>
    </body>
</html>
8
  • 1
    What is the current value of the display_errors INI setting?
    – salathe
    Mar 25, 2011 at 21:00
  • I've just check and it is set to error_reporting = E_ALL & ~E_DEPRECATED Mar 25, 2011 at 21:05
  • Please re-read my question, we already know your error_reporting setting because it is in the script.
    – salathe
    Mar 25, 2011 at 21:15
  • Please see UPDATE 1: in the original question. Mar 25, 2011 at 21:23
  • 2
    Please re-re-read my question. display_errors is entirely different from error_reporting. Also, your script overrides the error_reporting value set in the INI file.
    – salathe
    Mar 25, 2011 at 21:27

6 Answers 6

1306

Display errors could be turned off in the php.ini or your Apache configuration file.

You can turn it on in the script:

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

You should see the same messages in the PHP error log.

12
  • 2
    Do I need to restart apache after changing the php.ini file? Mar 25, 2011 at 21:10
  • 1
    already this question has been posted stackoverflow.com/questions/1053424/…
    – Elangovan
    Dec 3, 2014 at 11:55
  • 4
    @oshirowanen ini_set() does not actually change the php.ini file, the script only "acts" like it is set like that in php.ini... From manual: Sets the value of the given configuration option. The configuration option will keep this new value during the script's execution , and will be restored at the script's ending .
    – jave.web
    Dec 8, 2014 at 22:04
  • 5
    I don't know how many times I came to same answer. You helped me a lot. Thank you! :) Mar 12, 2015 at 8:32
  • 1
    @Charles Why would E_ALL remove E_NOTICE? Doesn't E_ALL by itself including everything, including E_NOTICE? Jul 21, 2016 at 18:28
70

I was able to get all errors via the below code:

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
4
  • only code that worked for me on my server (IONOS) Jul 12, 2019 at 14:23
  • 1
    error_reporting(-1); should not be used. It may not work on every system. Instead should be error_reporting(E_ALL);
    – JSowa
    May 2, 2020 at 11:26
  • error_reporting(-1) should work just fine as it is given as example here: php.net/manual/en/function.error-reporting.php May 16, 2020 at 11:03
  • 1
    The values for ini_set should actually be strings, such as ini_set('display_errors', '1'); as per documentation and this is enforced when using strict typing (eg, declare(strict_types=1);)
    – ashleedawg
    Sep 25, 2020 at 21:08
47

PHP errors can be displayed by any of below methods:

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

For more details:

Displaying PHP errors

1
  • 6
    The display_errors = on; in PHP file has syntax error. Jul 23, 2017 at 15:38
42

Straight from the php.ini file:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; This directive informs PHP of which errors, warnings and notices you would like
; it to take action for. The recommended way of setting values for this
; directive is through the use of the error level constants and bitwise
; operators. The error level constants are below here for convenience as well as
; some common settings and their meanings.
; By default, PHP is set to take action on all errors, notices and warnings EXCEPT
; those related to E_NOTICE and E_STRICT, which together cover best practices and
; recommended coding standards in PHP. For performance reasons, this is the
; recommend error reporting setting. Your production server shouldn't be wasting
; resources complaining about best practices and coding standards. That's what
; development servers and development settings are for.
; Note: The php.ini-development file has this setting as E_ALL. This
; means it pretty much reports everything which is exactly what you want during
; development and early testing.
;
; Error Level Constants:
; E_ALL             - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
; E_ERROR           - fatal run-time errors
; E_RECOVERABLE_ERROR  - almost fatal run-time errors
; E_WARNING         - run-time warnings (non-fatal errors)
; E_PARSE           - compile-time parse errors
; E_NOTICE          - run-time notices (these are warnings which often result
;                     from a bug in your code, but it's possible that it was
;                     intentional (e.g., using an uninitialized variable and
;                     relying on the fact it is automatically initialized to an
;                     empty string)
; E_STRICT          - run-time notices, enable to have PHP suggest changes
;                     to your code which will ensure the best interoperability
;                     and forward compatibility of your code
; E_CORE_ERROR      - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING    - warnings (non-fatal errors) that occur during PHP's
;                     initial startup
; E_COMPILE_ERROR   - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR      - user-generated error message
; E_USER_WARNING    - user-generated warning message
; E_USER_NOTICE     - user-generated notice message
; E_DEPRECATED      - warn about code that will not work in future versions
;                     of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings
;
; Common Values:
;   E_ALL (Show all errors, warnings and notices including coding standards.)
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices)
;   E_ALL & ~E_NOTICE & ~E_STRICT  (Show all errors, except for notices and coding standards warnings.)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT

For pure development I go for:

    error_reporting = E_ALL ^ E_NOTICE ^ E_WARNING

Also don't forget to put display_errors to on

display_errors = On

After that, restart your server for Apache on Ubuntu:

sudo /etc/init.d/apache2 restart
2
  • 7
    "PHP 6.0.0"? Heh :-)
    – Bell
    Oct 29, 2016 at 0:02
  • 1
    “For pure development @JohnMagnolia go[es] for: error_reporting = E_ALL ^ E_NOTICE ^ E_WARNING.” Are the ^ symbols concatenating the three options? If so, doesn’t E_ALL already include E_NOTICE and E_WARNING?
    – Lucas
    Apr 4, 2019 at 21:54
11

Set these on php.ini:

;display_startup_errors = On
display_startup_errors=off
display_errors =on
html_errors= on

From your PHP page, use a suitable filter for error reporting.

error_reporting(E_ALL);

Filers can be made according to requirements.

E_ALL
E_ALL | E_STRICT
7

You can see a detailed description here.

ini_set('display_errors', 1);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

Changelog

  • 5.4.0 E_STRICT became part of E_ALL

  • 5.3.0 E_DEPRECATED and E_USER_DEPRECATED introduced.

  • 5.2.0 E_RECOVERABLE_ERROR introduced.

  • 5.0.0 E_STRICT introduced (not part of E_ALL).

Not the answer you're looking for? Browse other questions tagged or ask your own question.