实例讲解PHP页面静态化

页面静态化,顾名思义是将动态的PHP转化为静态的Html,流程如下图

实例讲解PHP页面静态化_第1张图片

用户访问index.php,如果存在index.html且在有效期内,则直接输出index.html,否则去生成index.html

file_put_contents()输出静态文件

ob_start()开启PHP缓冲区

ob_get_contents()获取缓冲区内容

ob_clean()清空缓冲区

ob_get_clean()相当于ob_get_contents()+ob_clean()

代码示例

select('user', ['uid', 'username', 'email']);
 // 引入模板
 require_once "./templates/index.php";
 // 写入html
 file_put_contents('./html/index.html', ob_get_contents());
}

你可能感兴趣的:(实例讲解PHP页面静态化)