php(总结)


语法基础

1.文件

(1) 文件操作模式

操作符模式名称描述
r读模式打开文件,从头开始, **注意: ** 文件必须存在
r+读写模式打开文件,从头开始读写, 注意: 文件必须存在
w只写模式打开文件,从文件头开始写文件, 注意: 文件如果存在,则会清空文件;如果不存在,会自动创建文件
w+读写模式同上
a追加在文件的末尾进行写操作
a+追加在文件末尾进行读写操作
注意
注意; 在操作符后面加上b,表示该文件会以二进制的方式读或者写

(2) 文件目录

主要函数:

  • $ opendir(目录名)获取文件目录
  • str readdir($)返回当前文件下一个文件

实例:

<!DOCTYPE html>
<html>
  <head>
    <title>首页</title>
    <meta name="viewport" content="width=device-width, user-scalable=no">
    <style>
      *{
        margin:0;
        padding:0;
      }
      h1{
        font-size:80px;
        text-align:center;
      }
      li{
        font-size:30px;
        margin-left:80px;
        margin-top:20px;
      }
    </style>
  </head>
  <body>
    <h1>目录</h1>
    <div>
      <ol>
        <?php
        $dir_name=opendir("../htdocs");
        while($file_name=readdir($dir_name)){
          if($file_name!=".."&&$file_name!="."){
            //第一个变量为域名,第二个变量为端口       $url='http://'.$_SERVER["SERVER_NAME"].':'.$_SERVER["SERVER_PORT"]."/".$file_name;
          echo '<li><a href="'.$url.'">'.$file_name.'</a></li>';
          }
        }
        ?>
      </ol>
    </div>
  </body>
</html>

加强版代码: 实现管理员登录,自适应

<!DOCTYPE html>
<html>
  <head>
    <title>首页</title>
    <meta name="viewport" content="width=device-width, user-scalable=no">
    <style>
      *{
        margin:0;
        padding:0;
      }
      html{
        font-size:100px;
      }
      h1{
        font-size:.8rem;
        text-align:center;
      }
      li{
        font-size:.3rem;
        margin-left:.8rem;
        margin-top:.2rem;
      }
      div:nth-child(2){
        margin-top:-.5rem;
      }
      input:nth-child(1){
        margin-left:.25rem;
        width:60%;
        height:.3rem;
        border-radius:8px;
        box-shadow:4px 4px 4px;
      }
      input:nth-child(2){
        width:20%;
        height:.3rem;
        box-shadow:4px 4px 4px;
        border-radius:8px;
      }
    </style>
  </head>
  <body>
    <h1>目录</h1>
    <div>
      <form name="input" method="get" action="./index.php">
        <input type="text" name="e" value="管理员密码">
        <input type="submit" value="登录">
      </form>
      <ol>
        <?php
        //中文编码声明
        header("Content-type:text/html,charset=utf-0p8");
        $dir_name=opendir("../htdocs");
        //设置数组,保存参数
        $contains=array();
        while($file_name=readdir($dir_name)){
          if($file_name!=".."&&$file_name!="."){
                         
                        //第一个变量为域名,第二个变量为端口
            $url='http://'.$_SERVER["SERVER_NAME"].':'.$_SERVER["SERVER_PORT"]."/".$file_name;
          //分割字符串
          $expanded_name=explode('.',$file_name)[1];
          //将信息存入数组中
          $contains[count($contains)]=array($url,$file_name,$expanded_name);
         }
        }
        for($i=0;$i<count($contains);$i++){
          if($_GET["e"]=="cuihao"){
            echo '<li><a href="'.$contains[$i][0].'">'.$contains[$i][1].'</a></li>';
          }else{
            if($contains[$i][2]!="php"){
            echo '<li><a href="'.$contains[$i][0].'">'.$contains[$i][1].'</a></li>';
            }
          }
        }
        ?>
      </ol>
    </div>
  </body>
  <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
  <script>
  /**屏幕适配**/
  function custom_screen(){
    //获取html,html的fontSize,屏幕大小
    var element_html=document.documentElement;
    //alert(element_html);
    var font_html=window.getComputedStyle(element_html);
    //alert(font_html.fontSize);
    var screen_width=document.body.clientWidth;
    //alert(screen_width);
    
    //设置比例
    element_html.fontSize=39300/screen_width+"px";
  }
  
  /**主函数**/
  function main(){
    custom_screen();
    alert("执行完毕");
  }
  
  /**执行**/
  $(document).ready(function(){
    main();
  })
  </script>
</html>

(3) 读写文件

  1. $fp=fopen(“filename”,“w”)
  2. fread($fp,filesize(filename))
  3. fwrite($fp,text)
  4. fclose($fp)

2.页面缓存

  • ob_flush()

将缓存的数据释放出来

  • flush()

将释放出来的数据显示在浏览器上

注意: 使用顺序为 ob_flush()->flush()

  • ob_get_contents() (获取缓存区中内容)

  • ob_get_length() (获取缓存区中长度)


网络编程

1. curl类库

(1) curl_init()

步骤:

  1. 创建curl句柄( curl_init() ),打开箱子。
  2. 添加属性( curl_setopt(句柄,键,值) ),往箱子中放入东西
  3. 发送请求( curl_exec(句柄) ),发送快递
  4. 结束会话( curl_close() ),结账

我把操作curl发送请求的过程比喻成了寄快递的过程。简单易懂,无论code多么复杂,但实际上都为这四个步骤,只不过对发送和请求的处理比较复杂。

(2) curl_multi()

由于php没有多线程模块,因此当网络请求过多时便会显得很软肋。接下来将介绍到网络并发模式 curl_multi()

步骤:

  1. 创建 init curl_init() 句柄
  2. 设置参数 ( curl_setopt(句柄,键,值) )
  3. 创建 inti curl_multi_init() 批句柄
  4. 向批句柄中添加请求 curl_multi_add_handle(批句柄,子句柄)
  5. 执行请求 int curl_multi_exec(批句柄,状态),通过do while判断状态是否大于0,是继续,否完成。
do { 
$mrc = curl_multi_exec($mh, $active); 
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

或者

do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while ($running > 0);

注意: 以上两种代码都是判断批句柄是否执行完成,但是第二种方法开销过大。同时也可以加上异常检验 int curl_errno(句柄) 和 string curl_error(句柄)

6.得到数据 string curl_multi_getcontent(子句柄),需要设置参数(CURLOPT_RETURNTRANSFER,true)

7.关闭句柄 curl_multi_close(批句柄)

(3)curl_setopt()

  • curl_setopt($ch,CURLOPT_NOPROGRESS,1)
  • curl_setopt($ch,CURLOPT_PROGRESSFUNCTION,“function”)

分析: 以上两个参数可以实现获取加载进程,function回调函数中的参数有先后顺序,即:

/* * $ch
   * $countDownloadSize 总下载量
   * $currentDownloadSize 当前下载量
   * $countUploadSize 
   * $currentUploadSize
* */
  • curl_setopt($ch,CURLOPT_RETURNTRANSFER,1)

分析: 该属性设置获取数据不输出在浏览器,若要输出,可设置为0。

2. Snoopy类库

注意: 在使用Snoopy时,需要修改配置文件,找到extent php_openill,去掉前面的;号

使用步骤:

  1. 创建Snoopy对象
  2. 添加Snoopy对象属性
  3. 开始调用fetch方法,发送求情
  4. 得到结果(Snoopy对象的属性results)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值