Output Buffer shows “1”

回眸只為那壹抹淺笑 提交于 2019-12-11 05:06:05

问题


I have two functions:

core_function($atts) {
        (attributes)
        (core functions, a few loops, echoes, a lot of direct input)
    }

And that's how I display my function using output buffering (yes, I have to use it!).

display_function($atts) {
            (attributes)

                $output = ob_start();
                $output .= core_function($atts);
                $output .= ob_get_clean();

            return $output;
}

Everything is perfectly fine, but return $output shows not only core functions but also "1" before them. I have no idea where this "1" comes from. When I deletete ob_start(); and ob_get_clean(); it disappears. So I believe the output buffer is somehow adding this digit. But how, and why? It's a raw "1", not in a paragraph etc.

Normaly display_function($atts) shows, for example:

<div>This is Core Function!</div>

And with output buffering it displays:

1             <div>This is Core Function!</div>

Why is it happening? If it has something to do with my functions I'm saying again - the 1 is being displayed exactly BEFORE all the contents.


回答1:


That is not how output buffering works. ob_start returns TRUE or FALSE upon completion so you are concatenating a bunch of things that shouldn't be concatenated. (The same goes for your call to core_function).

display_function($atts) {
     (attributes)

     ob_start();
     core_function($atts);
     return ob_get_clean();
}

Should work. It turns on output buffering which will save all of your output (echo's and prints etc). The call to ob_get_clean will return the contents of your buffered output.



来源:https://stackoverflow.com/questions/5161626/output-buffer-shows-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!