加入收藏 | 设为首页 | 会员中心 | 我要投稿 源码门户网 (https://www.92codes.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP编程学习资料

发布时间:2022-01-20 09:48:03 所属栏目:PHP教程 来源:互联网
导读:文章主要是一个站长在学习php过程中一些用到的函数与方法及对各种方法的理解与简单的实例,下面全部放出来希望对你学习php有帮助. PHP使用header函数设置HTTP头的示例方法,代码如下: //定义编码 header( Content-Type:text/html;charset=utf-8 ); //Atom he
  文章主要是一个站长在学习php过程中一些用到的函数与方法及对各种方法的理解与简单的实例,下面全部放出来希望对你学习php有帮助.
 
  PHP使用header函数设置HTTP头的示例方法,代码如下:
 
  //定义编码
  header( Content-Type:text/html;charset=utf-8 );
  //Atom
  header(Content-type: application/atom+xml);
  //CSS
  header(Content-type: text/css);
  //Javascript
  header(Content-type: text/javascript);
  //JPEG Image
  header(Content-type: image/jpeg);
  //JSON
  header(Content-type: application/json);
  //PDF
  header(Content-type: application/pdf);
  //RSS
  header(Content-Type: application/rss+xml; charset=ISO-8859-1);
  //Text (Plain)
  header(Content-type: text/plain);
  //XML
  header(Content-type: text/xml);
  // ok
  header(HTTP/1.1 200 OK);
  //设置一个404头:
  header(HTTP/1.1 404 Not Found);
  //设置地址被永久的重定向
  header(HTTP/1.1 301 Moved Permanently);
  //转到一个新地址
  header(Location: http://www.example.org/);
  //文件延迟转向:
  header(Refresh: 10; url=http://www.example.org/);
  print You will be redirected in 10 seconds;
  //当然,也可以使用html语法实现
  // <meta http-equiv="refresh" content="10;http://www.example.org/ />
  // override X-Powered-By: PHP:
  header(X-Powered-By: PHP/4.4.0);
  header(X-Powered-By: Brain/0.6b);
  //文档语言
  header(Content-language: en);
  //告诉浏览器最后一次修改时间
  $time = time() - 60; // or filemtime($fn), etc
  header(Last-Modified: .gmdate(D, d M Y H:i:s, $time). GMT);
  //告诉浏览器文档内容没有发生改变
  header(HTTP/1.1 304 Not Modified);
  //设置内容长度
  header(Content-Length: 1234);
  //设置为一个下载类型
  header(Content-Type: application/octet-stream);
  header(Content-Disposition: attachment; filename="example.zip");
  header(Content-Transfer-Encoding: binary);
  // load the file to send:
  readfile(example.zip);
  // 对当前文档禁用缓存
  header(Cache-Control: no-cache, no-store, max-age=0, must-revalidate);
  header(Expires: Mon, 26 Jul 1997 05:00:00 GMT); // Date in the past
  header(Pragma: no-cache);
  //设置内容类型:
  header(Content-Type: text/html; charset=iso-8859-1);
  header(Content-Type: text/html; charset=utf-8);
  header(Content-Type: text/plain); //纯文本格式
  header(Content-Type: image/jpeg); //JPG***
  header(Content-Type: application/zip); // ZIP文件
  header(Content-Type: application/pdf); // PDF文件
  header(Content-Type: audio/mpeg); // 音频文件
  header(Content-Type: application/x-shockw**e-flash); //Flash动画
  //显示登陆对话框
  header(HTTP/1.1 401 Unauthorized);
  header(WWW-Authenticate: Basic realm="Top Secret");
  print Text that will be displayed if the user hits cancel or ;
  print enters wrong login data;
  php中static静态变量的使用方法详解
 
  php中的变量作用范围的另一个重要特性就是静态变量(static 变量),静态变量仅在局部函数域中存在且只被初始化一次,当程序执行离开此作用域时,其值不会消失,会使用上次执行的结果.
 
  编程实例,代码如下:
 
  function test()  
  {  
    static $aa = 0;  
    return $aa++;
  }  
  $aa = "1000";
  echo $aa;
  echo test();
  echo test();
  echo $aa;
  //本函数每调用test()都会输出 $aa的值并加一。
  //上文代码运行输出:
  //1000
  //0
  //1
  //1000
  静态变量也提供了一种处理递归函数的方法,递归函数是一种自己调用自己的方法,写递归函数时要小心,因为可能会无穷递归下去,没有出口.务必确保,有方法来中止递归.
 
  一维数组按照元素或者键值分组变为二维数组
 
  有时候查询数据库记录后会对数据库查询结果进行分组,即将一维数组变为二维数组,方便调用使用(通常是json),代码如下:
 
  $arr = array(
      '0'=>array(
              'firmware'=>'f1',
              'version'=>'1',
          ),
      '1'=>array(
              'firmware'=>'f1',
              'version'=>'2',
          ),
      '2'=>array(
              'firmware'=>'f1',
              'version'=>'3',
          ),
      '3'=>array(
              'firmware'=>'f2',
              'version'=>'1',
          ),
      '4'=>array(
              'firmware'=>'f2',
              'version'=>'2',
          ),
      );
      $new_arr  =  array();
      foreach ($arr as $row ){
          $new_arr[$row['firmware']][] = $row['version'];
      }
      var_dump($new_arr);
  
  /*转换后
  Array
  (
      [f1] => Array
          (
              [0] => 1
              [1] => 2
              [2] => 3
          )
      [f2] => Array
          (
              [0] => 1
              [1] => 2
          )
  
  )*/
  PHP的静态绑定和动态绑定(private/public)
 
  子类Foo的对象调用了test()方法,test()方法调用了$this->testPrivate();这个$this此时应该是子类的引用,按理说应该调用子类的testPrivate()方法,实际上却调用了父类的testPrivate()方法.代码如下:
 
  class Bar  
  {
      public function test() {
          $this->testPrivate();
          $this->testPublic();
      }
  
      public function testPublic() {
          echo "Bar::testPublicn";
      }
       
      private function testPrivate() {
          echo "Bar::testPrivaten";
      }
  }
  
  class Foo extends Bar  
  {
      public function testPublic() {
          echo "Foo::testPublicn";
      }
       
      private function testPrivate() {
          echo "Foo::testPrivaten";
      }
  }
  
  $myFoo = new Foo();
  $myFoo->test();  
  // 运行结果
  // Bar->testPrivate  
  // Foo->testPublic
  这是PHP的动态绑定和静态绑定的一种情况.
 
  public是动态绑定,在编译期不绑定,所以在运行期调用父类的test()方法的时候,会解析为子类的public方法.
 
  而private是私有的,不会继承到子类,在编译期就绑定了,是一种“静态”的绑定(类似5.3后的self).
 
  与这个相关的是LSB,静态延迟绑定,PHP5.3因为有了这个特性之后,使PHP的OOP得到加强
 
  public:可以被继承,也可以被外部调用.
 
  private:不可以被继承,也不可以被外部调用.
 
  protected:可以被继承,但不能被外部调用.
 
  PHP三种运行方式mod_php5/cgi/fast-cgi
 
  a.通过HTTPServer内置的模块来实现,例如Apache的mod_php5,类似的Apache内置的mod_perl可以对perl支持;
 
  b.通过CGI来实现,这个就好比之前perl的CGI,该种方式的缺点是性能差,因为每次服务器遇到这些脚本都需要重新启动脚本解析器来执行脚本然后将结果返回给服务器,另一方面就是不太安全,该方面几乎很少使用了.
 
  c.最新出现一种叫做FastCGI.所谓FastCGI就是对CGI的改进,它一般采用C/S结构,一般脚本处理器会启动一个或者多个daemon进程,每次HTTPServer遇到脚本的时候,直接交付给FastCGI的进程来执行,然后将得到的结果(通常为html)返回给浏览器.
 
  该种方法的问题存在一个小问题是当遇到大流量的频繁请求的话,脚本处理器的daemon进程可能会超负荷从而变得很慢,甚至发生内存泄漏;
 
  但是比较起Apache的内置模块的方式的优点是由于Server和脚本解析器完全分开各负其责,因此服务器不再臃肿,可以专心地进行静态文件响 应或者将动态脚本解析器的结果返回给用户客户端,所以比较起Apache的内置模块方式,有时候性能要提高很多,有人测试可能会达到 Apache+mod_php的5~10倍.

(编辑:源码门户网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读