Question

I was working on this book and I found an unexpected situation.

When working in a PHP/HTML hybrid file, the book says, if I use PHP try/catch without the ob buffer thing, and if there's an error somewhere in the middle of the file, the PHP engine won't be able to reach the catch{} line since some output has already been sent to the browser. The book then says this situation could be fixed by using ob_start(), ob_end_clean() and ob_end_flush()

However, when I was playing with the book's sample code, the try/catch worked just fine without the ob buffer stuff. By saying just fine I mean if there's an exception the catch{} line could be reached and executed without a problem.

I used a Linode VPS for testing, the PHP version is 5.3.2. I set up the VPS with some ordinary Linode script.

Why is that? :)

Was it helpful?

Solution

That statement is nonsense. PHP's try..catch works just as you would expect, regardless of output that has or hasn't already been sent to the browser.

Seeing that even the errata to that book contains errors, I'd say it's simply not a very good book and am not surprised at all that it contains wrong statements. Just a very quick glance reveals:

In step 4, the third line of code should read as follows:

if ($_POST && isset($missing) && !empty($missing)) {

No, it should actually read:

if ($_POST && !empty($missing)) {

or

if ($_POST && $missing) {

The author apparently does not understand how to use empty.

Further:

An opening curly brace is missing at the end of line 4 of the code in step 2. It should read:

if (!@include('includes/connection.inc.php')) {

That reeks of bad practices and should be rewritten to:

if (!file_exists('includes/connection.inc.php')) {

or:

require_once 'includes/connection.inc.php';

I'm sure there's more... :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top