How to see php error in included file during output buffer?

Blank screen when using output buffer and syntax errors in included file.
PHP does not display errors from the output buffer.
How to see php output buffer syntax errors?

In my project, I have used @ to hide errors if the file does not exist. But if the file exists and has fatal errors, they would not show up either.

Here's some sample code.

<?php
$title = 'Some title';

ob_start();

@include ('body.tpl'); //I have some php here

$content = ob_get_clean();


?><!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?= $title; ?></title>
</head>
<body>
    <?= $content; ?>
</body>
</html>

      

+3


source to share


3 answers


One option is to override the error handlers and call ob_end (); before printing the thrown error / warning / exception. Another option is to check the error log frequently if you encounter odd behavior.



+2


source


Don't use @include ('body.tpl');

What I did: - (



0


source


Mmm ... You can use Exceptions like this:

$includeFile = 'blah.php';

ob_start();

try {

    if (!is_file($includeFile)) {
        throw new Exception('File not found');
    }
    include $includeFile;

} catch (Exception $e) {
    include 'error-content-page.php';
}

$content = ob_get_clean();

      

-1


source







All Articles