Home  >  Article  >  Backend Development  >  What is the static method in php

What is the static method in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-30 16:07:342600browse

How to achieve staticization in php: 1. Rewrite the access address and change it through the PATHINFO mode of the URL; 2. The site can be staticized through a certain program before the user accesses the site.

What is the static method in php

How to achieve static php:

During the development process of PHP site, because search engines search for PHP pages There is a certain difference between the inclusion of deer and HTML pages. For the promotion of the site or the needs of SEO, the site must be made static to a certain extent. Staticization does not mean that there are no animation and other elements in the page, but it means that the html code of the webpage is in the page. There is no need to execute server-side languages ​​​​such as PHP scripts. We can directly access the webpage. This is a static web page.

One way is to rewrite the access address, which can be changed through the PATHINFO mode of the URL. Make it look more like a static page. Therefore, there is a greater chance of being crawled and included by search engines, but it is more friendly to search engines and pseudo-static.

The second is that the site can be made static through a certain program before the user accesses the site. Generate static pages. When the user visits this page. Because you are accessing a static page, the access speed will be many times faster than accessing a dynamic page. The front-end performance is that the page loading speed becomes faster, and the back-end performance is that the database connection is reduced. It reduces the pressure on the database. The only disadvantage is that it takes up more hard disks and the hard disks are relatively cheaper.

Pure static is the way to generate HTML files. We need to enable PHP's own caching mechanism, that is, ob_start to enable caching. And there cannot be any output before ob_start, otherwise the operation will fail. Then we use the ob_get_contents function to get the content in the cache, which will return a string. The third function is ob_end_clean, which is used to clear the contents of the cache and close it. It returns True on success and False on failure.

<?php
//开启缓存
ob_start();
//第一步连接数据库
$conn = mysqli_connect("localhost","root","","bbs");
//第二步设置对应的字符编码
$setting = &#39;set names utf8&#39;;
mysqli_query($conn,$setting);
//第三步进行查询
$sql = &#39;SELECT * FROM user&#39;;
$result = mysqli_query($conn,$sql);
//第四步把查询结果转化为一个数组
$rows = mysqli_num_rows($result);
$sqldata = array();
for($i = 0;$i <$rows;$i ++){
    $sqldata[] = mysqli_fetch_assoc($result);
}
//然后打印该信息
var_dump($sqldata);
//得到生成的html文件,下次訪问就无需訪问数据库了
$msg = ob_get_contents();
ob_end_clean();
//把输出内容放入一个html文件里
$f = fopen("static.html","w");
fwrite($f,$msg);
echo "静态化成功";

Generate an html file in the directory

<pre class=&#39;xdebug-var-dump&#39; dir=&#39;ltr&#39;>
<b>array</b> <i>(size=6)</i>
  0 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;0&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;辛星&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;bd04fcc97578ce33ca5fb331f42bc375&#39;</font> <i>(length=32)</i>
  1 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;2&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;小倩&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;61cb72858be523b9926ecc3d7da5d0c6&#39;</font> <i>(length=32)</i>
  2 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;3&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;小楠&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;a3d2de7675556553a5f08e4c88d2c228&#39;</font> <i>(length=32)</i>
  3 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;4&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;刘强&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;fcdb06a72af0516502e5fdccc9181ee0&#39;</font> <i>(length=32)</i>
  4 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;5&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;星哥&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;866a6cafcf74ab3c2612a85626f1c706&#39;</font> <i>(length=32)</i>
  5 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;6&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;辛勇&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;e93beb7663f3320eaa0157730d02dd0c&#39;</font> <i>(length=32)</i>

Related learning recommendations: php programming (video)

The above is the detailed content of What is the static method in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn