zoukankan      html  css  js  c++  java
  • 摘抄php很有用的10个函数

      1 1. sys_getloadavg()
      2 
      3 sys_getloadavt()可以获得系 统负载情况。该函数返回一个包含三个元素的数组,每个元素分别代表系统再过去的1、5和15分钟内的平均负载。与其让服务器因负 载过高而宕掉,不如在系统负载很高时主动die掉一个脚本,sys_getloadavg()就是用来帮你实现这个功能的。 不过很遗憾,该函数在windows下无效。
      4 
      5 2. pack()
      6 
      7 Pack() 能将md5()返回的32位16进制字符串转换为16位的二进制字符串,可以节省存储空间。
      8 
      9 3. cal_days_in_month()
     10 
     11 cal_days_in_month()能够返回指定月份共有多少天。
     12 
     13 4. _()
     14 
     15 WordPress开发者经常能见到这个函数,还有 _e()。这两个函数功能相同,与gettext()函数结合使用,能实现网站的多语言化。具体可参见PHP手册的相关部分介绍。
     16 
     17 5. get_browser()
     18 
     19 在发送页面前先看看用户的浏览器都能做些什么是 不是挺好?get_browser()能获得用户的浏览器类型,以及浏览器支持的功能,不过首先你需要一个php_browscap.ini文件,用来给 函数做参考文件。
     20 
     21 要注意,该函数对浏览器功能的判断是基于该类浏览器的一般特性的。例如,如果用户关闭了浏览器对 JavaScript的支持,函数无法得知这一点。但是在判断浏览器类型和OS平台方面,该函数还是很准确的。
     22 
     23 6. debug_print_backtrace()
     24 
     25 这是一个调试用的函数,能帮助你发现代码中的逻辑错误。要理 解这个函数,还是直接看个例子吧:
     26 
     27 $a = 0;   
     28 function iterate() {   
     29 global $a;   
     30 if( $a < 10 )   
     31 recur();   
     32 echo $a . “, “;   
     33 }   
     34 function recur() {   
     35 global $a;   
     36 $a++;   
     37 // how did I get here?   
     38 echo “\n\n\n”;   
     39 debug_print_backtrace();   
     40 if( $a < 10 )   
     41 iterate();   
     42 }   
     43 iterate();   
     44 # OUTPUT:   
     45 #0 recur() called at [C:\htdocs\php_stuff\index.php:8]   
     46 #1 iterate() called at [C:\htdocs\php_stuff\index.php:25]   
     47 #0 recur() called at [C:\htdocs\php_stuff\index.php:8]   
     48 #1 iterate() called at [C:\htdocs\php_stuff\index.php:21]   
     49 #2 recur() called at [C:\htdocs\php_stuff\index.php:8]   
     50 #3 iterate() called at [C:\htdocs\php_stuff\index.php:25]   
     51 #0 recur() called at [C:\htdocs\php_stuff\index.php:8]   
     52 #1 iterate() called at [C:\htdocs\php_stuff\index.php:21]   
     53 #2 recur() called at [C:\htdocs\php_stuff\index.php:8]   
     54 #3 iterate() called at [C:\htdocs\php_stuff\index.php:21]   
     55 #4 recur() called at [C:\htdocs\php_stuff\index.php:8]   
     56 #5 iterate() called at [C:\htdocs\php_stuff\index.php:25]  
     57 7. metaphone()
     58 
     59 这个函数返回单词的metaphone值,相同读音的单词具有相同的metaphone值,也就是说这个函数可以帮你判断两个单词的读音是否 相同。
     60 
     61 8. natsort()
     62 
     63 natsort()能将一个数组以自然排序法 进行排列,直接看个例子吧:
     64 
     65 $items = array(   
     66 “100 apples”, “5 apples”, “110 apples”, “55 apples”   
     67 );   
     68 // normal sorting:   
     69 sort($items);   
     70 print_r($items);   
     71 # Outputs:   
     72 # Array   
     73 # (   
     74 # [0] => 100 apples   
     75 # [1] => 110 apples   
     76 # [2] => 5 apples   
     77 # [3] => 55 apples   
     78 # )   
     79 natsort($items);   
     80 print_r($items);   
     81 # Outputs:   
     82 # Array   
     83 # (   
     84 # [2] => 5 apples   
     85 # [3] => 55 apples   
     86 # [0] => 100 apples   
     87 # [1] => 110 apples   
     88 # )  
     89 9. levenshtein()
     90 
     91 Levenshtein()告诉你两个单词之间的“距离”。它告诉你如果想把一个单词变成另一个单词,需要插入、替换和删除多少字母。
     92 
     93 看个例子吧:
     94 
     95 $dictionary = array(   
     96 “php”, “javascript”, “css”   
     97 );   
     98 $word = “japhp”;   
     99 $best_match = $dictionary[0];   
    100 $match_value = levenshtein($dictionary[0], $word);   
    101 foreach($dictionary as $w) {   
    102 $value = levenshtein($word, $w);   
    103 if( $value < $match_value ) {   
    104 $best_match = $w;   
    105 $match_value = $value;   
    106 }   
    107 }   
    108 echo “Did you mean the ‘$best_match’ category?”;  
    109 10. glob()
    110 
    111 glob()会让你觉得用 opendir(), readdir()和closedir()来寻找文件非常蠢。
    112 
    113 foreach (glob(“*.php”) as $file)   
    114 echo$file\n”; 

    1、is_a($obj,$class)判断是否为一个类的实例化对象

    2、strtotime()转换的是以时间秒为单位的

    3、array_search($val);知道键值,返回他的键名,对于索引和关联都起作用,但是要注意,如果他的键名是‘0’,一定要记得用!==false,因为php是弱变量的,所以我们一定要记得用不全等于false


    几个PHP函数:

    is_a:Checks if the object is of this class or has this class as one of its parents (return bool)

     1 <?php
     2 // define a class
     3 class WidgetFactory
     4 {
     5   var $oink = 'moo';
     6 }
     7 
     8 // create a new object
     9 $WF = new WidgetFactory();
    10 
    11 if (is_a($WF, 'WidgetFactory')) {
    12   echo "yes, \$WF is still a WidgetFactory\n";
    13 }
    14 ?>

    get_object_vars:get_object_vars — Gets the properties of the given object (return array)

     1 <?php
     2 
     3 class foo {
     4     private $a;
     5     public $b = 1;
     6     public $c;
     7     private $d;
     8     static $e;
     9    
    10     public function test() {
    11         var_dump(get_object_vars($this));
    12     }
    13 }
    14 
    15 $test = new foo;
    16 var_dump(get_object_vars($test));
    17 
    18 $test->test();
    20 ?>
    21 
    22 will output:
    23 
    24 array(2) {
    25   ["b"]=> int(1)
    27   ["c"]=>  NULL
    29 }
    30 array(4) {
    31   ["a"]=> NULL
    33   ["b"]=>int(1)
    35   ["c"]=>  NULL
    37   ["d"]=>  NULL
    39 }

    property_exists: Checks if the object or class has a property (return bool)

     1 <?php
     2 
     3 class myClass {
     4     public $mine;
     5     private $xpto;
     6     static protected $test;
     7 
     8     static function test() {
     9         var_dump(property_exists('myClass', 'xpto')); //true
    10     }
    11 }
    12 
    13 var_dump(property_exists('myClass', 'mine'));   //true
    14 var_dump(property_exists(new myClass, 'mine')); //true
    15 var_dump(property_exists('myClass', 'xpto'));   //true, as of PHP 5.3.0
    16 var_dump(property_exists('myClass', 'bar'));    //false
    17 var_dump(property_exists('myClass', 'test'));   //true, as of PHP 5.3.0
    18 myClass::test();
    19 
    20 ?>

    sprintf()函数,自己看看,有时候用起来会比较简单,尤其是一句sql里面有多个参数变量的时候,非常简单

    1 __DIR__得到当前的目录,php5.3以后的版本
    2 uniqid : generate unique ID]
    3 json_encode:转换成json字符串
    4 json_decode :进行解码,返回原来的格式
    5 
    6 压缩字符串:()节省空间
    7     gzencode()       
    8     gzcompress() 
    9  解压缩:
      gzdecode()
      gzuncompress()



  • 相关阅读:
    pkg_resources.DistributionNotFound: The 'catkin-pkg==0.4.9' distribution was not found
    gsl库安装
    json ubuntu下安装
    系统安装情况以及深度学习环境搭建
    ros 编程习惯
    ubuntu系统ftp连接 以及ssh连接
    redmine问题
    maven仓库私服配置
    SVN配置管理(trunk、branches、tags)
    Gitolite配置管理和GIT基本操作
  • 原文地址:https://www.cnblogs.com/zhangjun516/p/2887250.html
Copyright © 2011-2022 走看看