简体   繁体   English

PHP ob_start无效

[英]PHP ob_start not working

I am new to PHP and Code Ignitor, Facing some issue while trying to convert dynamic data content into static html file. 我是PHP和Code Ignitor的新手,在尝试将动态数据内容转换为静态html文件时面临一些问题。 This is code snippet. 这是代码段。 when i request code snippet file it prints only Error 111111111 nothing else. 当我请求代码片段文件时,它只打印错误111111111。 not able to understand what is the error here. 无法理解这里的错误是什么。

This is my original Code and here am trying to generate static html file with dynamic content. 这是我原来的代码,这里我试图生成带有动态内容的静态html文件。 It doesn't works for me 它不适合我

<?php
    echo "Error 111111111";
    ob_start();
    $fileName = "sample.html";
?>
<html>
<body>
    Some html is here       
</body>
</html>
<?php
    try{ 
           $output = ob_get_contents(); // get contents of trapped output

            //write to file, e.g.
           $newfile = $fileName; 
           $file = fopen ($newfile, "w"); 
           fwrite($file, $output); 
           fclose ($file);  
           ob_end_clean(); // discard trapped output and stop trapping
    }catch (Exception $ex){                
      echo "Error ".$ex->getMessage();
    }      

?>

I don't see an error? 我没有看到错误?

ob_start() suppresses all output until ob_flush() is called. ob_start()在调用ob_flush()之前抑制所有输出。 You're not calling ob_flush() , so nothing after the ob_start() will be output. 你没有调用ob_flush() ,所以在输出ob_start()之后什么也没有。 That's what you're seeing, and that's exactly the way it's supposed to work. 这就是你所看到的,这正是它应该工作的方式。

I suppose the real question is what were you trying to achieve? 我想真正的问题是你想要实现的目标是什么?

The code snippet is quite confusing, because ob_start() doesn't generate any exceptions, yet you've put it into a try / catch block. 代码片段非常混乱,因为ob_start()不会生成任何异常,但您已将其放入try / catch块中。 Your catch section will never be called because nothing in the try block will ever generate any exceptions. 永远不会调用您的catch部分,因为try块中的任何内容都不会生成任何异常。

So what were you trying to do here? 那你在这里做什么? The answer to that may help us give you more guidance. 答案可能有助于我们为您提供更多指导。

ob_start标记缓冲输出应该从哪里开始,但是AFAIK你还必须告诉PHP结束缓冲并输出当前内容: ob_end_flush()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM