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(); ?>













猜你喜欢

转载自www.cnblogs.com/hehexu/p/9190417.html
ob
今日推荐