dongliu5475 2012-05-25 02:35
浏览 50
已采纳

PHP Try Catch Buffer

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

  • 写回答

1条回答 默认 最新

  • drui0508 2012-05-25 04:27
    关注

    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... :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度