3

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>
mevsme
  • 183
  • 9
  • 1
    You can't. output buffering catches ALL output, whether it's from your code or something internal to PHP generating it. You'll have to either disable the OB system while debugging, or [log to file](http://stackoverflow.com/questions/3531703/how-to-log-errors-and-warnings-into-a-file) – Marc B Sep 11 '14 at 19:38
  • I see. But can you point me (some urls) how php (apache?) error throwing works? I can not get why regular php script shows diff. errors\warning, etc. But included file not have been showing? Why it's impossible ` = $title; ?>Warning:include(data/menu.tpl) [function.include]: blabla in \body.tpl on line 3` – mevsme Sep 11 '14 at 19:59
  • ob also automatically shuts down and flushes output when the script exits. the only time you wouldn't get any output from ob is if the script terminates abnormally (e.g. internal fatal error, out of memory, etc...) – Marc B Sep 11 '14 at 20:20

3 Answers3

2

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.

Lauri Orgla
  • 542
  • 3
  • 10
  • Thanks for answer, but I need to `override error handlers` in parent or including file? – mevsme Sep 11 '14 at 19:49
  • In the beginning of your program's bootstrap file. i.e index.php. Take look at: http://php.net/manual/en/function.set-error-handler.php – Lauri Orgla Sep 11 '14 at 19:51
  • If this solved your problem, then please accept the answer. There are so many unaccepted but solved questions in stackoverflow. – Lauri Orgla Sep 11 '14 at 19:52
  • Ok, I've read that article few times, but still don't get, how to use it. For example, I'll take the one from 1st example. function myErrorHandler($errno, $errstr, $errfile, $errline) ... Than I have to triger some errors. But how missed semicolon will triger **Parse error**? – mevsme Sep 11 '14 at 20:04
  • The reason to that, is that PHP is being tokenized first, and then sent thru compiler which makes OPCODe, when this compiler finds out, that you have a mistake, or broken token, then it triggers parser error. This parser error can not be caught by a overriden error handler. since PHP code is never being execute. Its just shouting at you for having a syntax error. – Lauri Orgla Sep 11 '14 at 20:12
  • **@Lauri Orgla** Please, give me a short summery: if I want to edit php code in `body.tpl` and make some syntax error, there is no chanse to see where I've made it, if I useing ob_start() programming approach for this particular case? – mevsme Sep 11 '14 at 20:25
  • Try setting error_reporting(E_ALL); and ini_Set('display_errors', 1); I tested ur code, and if i made a syntax error in body.tpl, then it worked flawleslly. – Lauri Orgla Sep 11 '14 at 20:30
  • **@Lauri Orgla** Yes, You're right, it really shows. What is strange, becouse my example is an simplified version of my bigger project. And it still shows blank screen -_- Gonne dig it. – mevsme Sep 11 '14 at 20:41
  • OMFG) I've found the answer!) – mevsme Sep 11 '14 at 20:43
  • If my answers helped you, then feel free to accept the solution :) – Lauri Orgla Sep 11 '14 at 20:44
  • **@Lauri Orgla** Thanks a lot, you really helped me to find out the problem by pointing at my own example. I thinks that your answer is great for general issues of this type. – mevsme Sep 11 '14 at 20:52
0

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

mevsme
  • 183
  • 9
  • Jep, @ suppresses errors, so they wont be shown. Using @ is considered a bad practice, which leads to dirty and probably unstable/unsecure code. – Lauri Orgla Sep 11 '14 at 20:56
  • Like protestation: php.ini used to have session.gc_probability=0 with the comment: "This is disabled in the Debian packages, due to the strict permissions on /var/lib/php5". The strict permissions remain, but session.gc_probability is now enabled. By default there's a 0.1% chance that a call to session_start() will trigger this, but setting session.gc_divisor=1 makes this easily reproducible. http://somethingemporium.com/2007/06/obscure-error-with-php5-on-debian-ubuntu-session-phpini-garbage and make it: @ session_start(); – Deep Sep 11 '14 at 21:15
-1

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();
Deep
  • 2,126
  • 2
  • 12
  • 22
  • There is no example, of what is in error-content-page.php. If you DONT have ob_end(); or ob_get_clean(); before echoing out exceptions, then they will still be invisible. since ob_start(); has been started already. – Lauri Orgla Sep 11 '14 at 20:07
  • of course - this is only example. But if TS want catch all errors - his need create error handler and use him. – Deep Sep 11 '14 at 20:11
  • **@Deep** It's not my case, if file doesn't exist, php will show error anyway. If you use `include`, you'll get `Warning` and `html` without `$content`. My case is, when file exist, but there's, for example, syntax error. – mevsme Sep 11 '14 at 20:12