How show all errors in php?

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 } ?>

        <h2>Register</h2>

        <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>

A PHP application may produce many different levels of errors and warnings when executed. Viewing these errors is critical for developers to troubleshoot an application. However, difficulties are often encountered when trying to display errors from PHP applications, which often fail silently.

Quickest Way to Show All PHP Errors

Adding the following lines to your PHP code is the quickest way to show all PHP errors and warnings:

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

The above functions are directives work as follows:

ini_set()

The ini_set function tries to override the configuration found in the PHP .ini file.

display_errors

The display_errors is a directive that determines whether the errors will be shown to the user or remain hidden. This should usually be disabled after development.

display_startup_errors

The display_startup_errors is also a directive, which is used to find the errors encountered during the PHP startup sequence. This is a separate directive because the display_errors directive does not handle such errors.

Unfortunately, the display_errors and display_startup_errors directives do not show parse errors such as missing semicolons or curly braces. The PHP ini configuration must be modified to achieve this.

error_reporting()

The error_reporting is a native PHP function that is used to show errors. This function can be used to report all types of errors in the PHP script. For that, the named constant E_ALL is used as the argument in the function.

Configure PHP.ini to Display All Errors and Warnings

If adding the above functions and directives does not show all errors, the PHP ini configuration has additional directives that can be modified:

display_errors = on

The display_errors directive can be set to "on" in the PHP.ini file. This will display all errors including syntax and parse errors that are not displayed by only calling the ini_set function in the PHP code.

Note that the display_errors directive must be set to "off" if the application is in production.

The PHP.ini file can be found in the output of the phpinfo() function:

#php 7.x
<?php
phpinfo();
?>

Loaded Configuration File shows the location of the PHP.ini file.

The PHP error_reporting() function

The error reporting function is a built-in function in PHP that allows developers to specify which and how many errors are shown in the application. This function sets the error_reporting directive in the PHP ini configuration during runtime.

error_reporting(0);

The value 0 should be passed to the error_reporting function to remove all errors, warnings, parse messages and notices.

error_reporting(E_NOTICE);

Variables are allowed to be used in PHP even if they are not declared. This is not best practice as undeclared variables cause issues if used in loops and conditions. Undeclared variables are displayed in the web application when E_NOTICE is passed in the error_reporting function.

error_reporting(E_ALL & ~E_NOTICE);

The error_reporting function allows developers to filter which PHP errors can be shown. The “~” character stands for "not" or "no", so the parameter ~E_NOTICE means not to show notices. The "&" character means "true for all".

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

These 3 lines of code do the same thing - they show all PHP errors. The error_reporting(E_ALL) is the most widely used since it is more readable.

Show PHP Errors Through .htaccess Configuration

Directory files are usually accessible to developers. The .htaccess file located in the root or public directory of the project can also be used to enable or disable the directive for showing PHP errors.

php_flag display_startup_errors on
php_flag display_errors on

The .htaccess file has directives for display_startup_errors and display_errors, similar to what will be added to the PHP code to show errors. Development and production can have different .htaccess files by showing or disabling error messages this way, with production suppressing the displaying of errors.

The display_errors directive in .htaccess or the PHP.ini file may need to be configured depending on which files are accessible and how deployments and server configurations are done. Many hosting providers will not allow changes to the PHP.ini file to enable display_errors.

Track, Analyze and Manage PHP Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing and showing PHP errors easier than ever. Try it today

How do I log errors in PHP?

To log errors in PHP, open the php. ini file and uncomment/add the following lines of code. If you want to enable PHP error logging in individual files, add this code at the top of the PHP file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

Which function is used to display the errors?

The display_startup_errors is also a directive, which is used to find the error during the startup sequence of PHP. The error_reporting is a native function of PHP. It is used to display errors. ... Error Report Level..

How can I see PHP errors in xampp?

To Show All PHP Errors ini_set ('display_errors', 1); ini_set ('display_startup_errors', 1); error_reporting (E_ALL); The ini_set() function will attempt to override the PHP ini file's configuration. The display_errors and display_startup_errors directives are just two of the options.

How many errors are there in PHP?

Basically there are four types of errors in PHP, which are as follows: Parse Error (Syntax Error) Fatal Error. Warning Error.