默认 PHP 如果 return 的内容为空则回显

Default PHP echo if the content to return is empty

让我们看看我能否解释一下自己。如果从服务器发送到 return 的内容为空,我需要 return 默认 HTML 模板作为回退操作。它应该是这样的:

<?php

    // Lots of code here

    // Here I could echo something
    // echo "Welcome world!";
    // but I will not so no text would be returned but ...

    // in this case I have this fallback function that will only be fired if
    // no text has been outputted by any mean before
    // fallback_template();

    fallback_template() {
        echo "<html><head><title>Empty</title></head><body>No content</body></html>";
    }

我试过使用 headers_sent() 来检查,但它不起作用。有什么方法可以实现吗?还是超出了 PHP 的可能性?我需要它用于 browserSync,因此它始终保持活动状态。

    <?php
        ob_start();
        // Lots of code here

        // Here I could echo something
        // echo "Welcome world!";
        // but I will not so no text would be returned but ...

        // in this case I have this fallback function that will only be fired if
        // no text has been outputted by any mean before
        // fallback_template();

        fallback_template() {
            // buffer is empty nothing was sent to client (using print, echo or an error happened)
            if(ob_get_length() == 0 ) {            
                echo "<html><head><title>Empty</title></head><body>No content</body></html>";
            }
        }