mevsme
mevsme

Reputation: 549

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

Blank screen when using output buffer and there's syntax errors in included file.
PHP doesn't show errors from output buffer.
How to see php output buffer syntax errors?

In my project I used @ for hiding errors if file doesn't exist. But if file does exist and has fatal errors, they would not been shown as well.

Here's code example.

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

Upvotes: 3

Views: 1350

Answers (3)

mevsme
mevsme

Reputation: 549

Don't use @include ('body.tpl'); What I did :-(

Upvotes: 0

Deep
Deep

Reputation: 2512

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();

Upvotes: -1

Lauri Orgla
Lauri Orgla

Reputation: 561

One option is to override error handlers, and call ob_end(); before printing out thrown error/warning/exception. Other option, is to frequently check error log, if you encounter odd behaviour.

Upvotes: 2

Related Questions