• ob 函数的使用


    ob 函数的使用
    1. 页面静态化

    $id = isset($_GET['id'])?$_GET['id']-0:0; $filename = "html/".date("Ymd")."/news-id".$id.".html"; if(!file_exists("html/".date("Ymd"))){ mkdir("html/".date("Ymd")); } echo $filename; if(!file_exists($filename) || filemtime($filename)+60<time()){ ob_start(); require "conn.php"; $sql = "select * from news where id='$id'"; $res = $db->query($sql); $row = $res->fetch_assoc(); if($row){ echo "<table border='1'>"; echo "<tr>"; echo "<td>ID</td>"; echo "<td>标题</td>"; echo "<td>内容</td>"; echo "</tr>"; echo "<tr>"; echo "<td>".$row['id']."</td>"; echo "<td>".$row['title']."</td>"; echo "<td>".$row['content']."</a></td>"; echo "</tr>"; echo "</table>"; }else{ echo "没有数据"; } $content = ob_get_contents(); ob_end_clean(); file_put_contents($filename,$content); } require $filename;



    2. 因为原来的php是按脚本顺序执行的 如果前面输出了内容 ,执行到后面又想对前面已经输出的内容要修改,这时候就有用
    用法示例 $buffer  为缓存内容
    <?php
    
    function callback($buffer)
    {
      // replace all the apples with oranges
      return (str_replace("apples", "oranges", $buffer));
    }
    
    ob_start("callback");
    
    ?>
    <html>
    <body>
    <p>It's like comparing apples to oranges.</p>
    </body>
    </html>
    <?php
    
    ob_end_flush();
    
    ?>













  • 相关阅读:
    MVC设计模式和三层架构
    Servlet使用反射机制
    JSP的工作原理
    Servlet相关的几种乱码
    Word发送至博客园
    使用为知笔记上传到博客园
    idea乱码问题
    Servlet快速入门及运行流程
    【转】SQL注入及mybatis中#{}和${}的区别
    我的这个时代与你们
  • 原文地址:https://www.cnblogs.com/hehexu/p/9190417.html
Copyright © 2020-2023  润新知