zoukankan      html  css  js  c++  java
  • php九个很有用的功能

    1.任意数目的参数

     // 两个默认参数的函数 
        function foo($arg1 = '', $arg2 = '') { 
          
            echo "arg1: $arg1
    "; 
            echo "arg2: $arg2
    "; 
          
        } 
          
        foo('hello','world'); 
        /* 输出: 
        arg1: hello 
        arg2: world 
        */ 
          
        foo(); 
        /* 输出: 
        arg1: 
        arg2: 
    
    现在我们来看一看一个不定参数的函数,其使用到了?func_get_args()方法:
    
        // 是的,形参列表为空 
        function foo() { 
          
            // 取得所有的传入参数的数组 
            $args = func_get_args(); 
          
            foreach ($args as $k => $v) { 
                echo "arg".($k+1).": $v
    "; 
            } 
          
        } 
          
        foo(); 
        /* 什么也不会输出 */ 
          
        foo('hello'); 
        /* 输出 
        arg1: hello 
        */ 
          
        foo('hello', 'world', 'again'); 
        /* 输出 
        arg1: hello 
        arg2: world 
        arg3: again 
        */ 

    2.glob()查找文件

     // 取得所有的后缀为PHP的文件 
        $files = glob('*.php'); 
          
        print_r($files); 
        /* 输出: 
        Array 
        ( 
            [0] => phptest.php 
            [1] => pi.php 
            [2] => post_output.php 
            [3] => test.php 
        ) 
        */ 
    
    你还可以查找多种后缀名
    
        // 取PHP文件和TXT文件 
        $files = glob('*.{php,txt}', GLOB_BRACE); 
          
        print_r($files); 
        /* 输出: 
        Array 
        ( 
            [0] => phptest.php 
            [1] => pi.php 
            [2] => post_output.php 
            [3] => test.php 
            [4] => log.txt 
            [5] => test.txt 
        ) 
        */ 
    
    你还可以加上路径:
    
        $files = glob('../images/a*.jpg'); 
          
        print_r($files); 
        /* 输出: 
        Array 
        ( 
            [0] => ../images/apple.jpg 
            [1] => ../images/art.jpg 
        ) 
        */ 
    
    如果你想得到绝对路径,你可以调用?realpath() 函数:
    
        $files = glob('../images/a*.jpg'); 
          
        // applies the function to each array element 
        $files = array_map('realpath',$files); 
          
        print_r($files); 
        /* output looks like: 
        Array 
        ( 
            [0] => C:wampwwwimagesapple.jpg 
            [1] => C:wampwwwimagesart.jpg 
        ) 
        */ 

    3.内存使用查看

    echo "Initial: ".memory_get_usage()." bytes 
    "; 
        /* 输出 
        Initial: 361400 bytes 
        */ 
          
        // 使用内存 
        for ($i = 0; $i < 100000; $i++) { 
            $array []= md5($i); 
        } 
          
        // 删除一半的内存 
        for ($i = 0; $i < 100000; $i++) { 
            unset($array[$i]); 
        } 
          
        echo "Final: ".memory_get_usage()." bytes 
    "; 
        /* prints 
        Final: 885912 bytes 
        */ 
          
        echo "Peak: ".memory_get_peak_usage()." bytes 
    "; 
        /* 输出峰值 
        Peak: 13687072 bytes 
    */

    5.CPU使用

    print_r(getrusage()); 
        /* 输出 
        Array 
        ( 
            [ru_oublock] => 0 
            [ru_inblock] => 0 
            [ru_msgsnd] => 2 
            [ru_msgrcv] => 3 
            [ru_maxrss] => 12692 
            [ru_ixrss] => 764 
            [ru_idrss] => 3864 
            [ru_minflt] => 94 
            [ru_majflt] => 0 
            [ru_nsignals] => 1 
            [ru_nvcsw] => 67 
            [ru_nivcsw] => 4 
            [ru_nswap] => 0 
            [ru_utime.tv_usec] => 0 
            [ru_utime.tv_sec] => 0 
            [ru_stime.tv_usec] => 6269 
            [ru_stime.tv_sec] => 0 
        ) 
          
        */ 
    
    这个结构看上出很晦涩,除非你对CPU很了解。下面一些解释:
    
        ru_oublock: 块输出操作
        ru_inblock: 块输入操作
        ru_msgsnd: 发送的message
        ru_msgrcv: 收到的message
        ru_maxrss: 最大驻留集大小
        ru_ixrss: 全部共享内存大小
        ru_idrss:全部非共享内存大小
        ru_minflt: 页回收
        ru_majflt: 页失效
        ru_nsignals: 收到的信号
        ru_nvcsw: 主动上下文切换
        ru_nivcsw: 被动上下文切换
        ru_nswap: 交换区
        ru_utime.tv_usec: 用户态时间 (microseconds)
        ru_utime.tv_sec: 用户态时间(seconds)
        ru_stime.tv_usec: 系统内核时间 (microseconds)
        ru_stime.tv_sec: 系统内核时间?(seconds)
    
    要看到你的脚本消耗了多少CPU,我们需要看看“用户态的时间”和“系统内核时间”的值。秒和微秒部分是分别提供的,您可以把微秒值除以100万,并把它添加到秒的值后,可以得到有小数部分的秒数。
    
        // sleep for 3 seconds (non-busy) 
        sleep(3); 
          
        $data = getrusage(); 
        echo "User time: ". 
            ($data['ru_utime.tv_sec'] + 
            $data['ru_utime.tv_usec'] / 1000000); 
        echo "System time: ". 
            ($data['ru_stime.tv_sec'] + 
            $data['ru_stime.tv_usec'] / 1000000); 
          
        /* 输出 
        User time: 0.011552 
        System time: 0 
        */ 
    
    sleep是不占用系统时间的,我们可以来看下面的一个例子:
    
        // loop 10 million times (busy) 
        for($i=0;$i<10000000;$i++) { 
          
        } 
          
        $data = getrusage(); 
        echo "User time: ". 
            ($data['ru_utime.tv_sec'] + 
            $data['ru_utime.tv_usec'] / 1000000); 
        echo "System time: ". 
            ($data['ru_stime.tv_sec'] + 
            $data['ru_stime.tv_usec'] / 1000000); 
          
        /* 输出 
        User time: 1.424592 
        System time: 0.004204 
        */ 
    
    这花了大约14秒的CPU时间,几乎所有的都是用户的时间,因为没有系统调用。
    
    系统时间是CPU花费在系统调用上的上执行内核指令的时间。下面是一个例子:
    
        $start = microtime(true); 
        // keep calling microtime for about 3 seconds 
        while(microtime(true) - $start < 3) { 
          
        } 
          
        $data = getrusage(); 
        echo "User time: ". 
            ($data['ru_utime.tv_sec'] + 
            $data['ru_utime.tv_usec'] / 1000000); 
        echo "System time: ". 
            ($data['ru_stime.tv_sec'] + 
            $data['ru_stime.tv_usec'] / 1000000); 
          
        /* prints 
        User time: 1.088171 
        System time: 1.675315 
        */ 
    
    我们可以看到上面这个例子更耗CPU。

    5.系统常量

    __LINE__ 行号
    __FIEL__文件
    __DIR__目录
    __FUNCTION__函数名
    __CLASS__类名
    __METHOD__方法名
    __NAMESPACE__命名空间

    6.唯一的ID

    有很多人使用md5(time() . mt_rand(1,1000000)); 来生成唯一的ID
    其实uniqid()就可以达到目的,而且前几位还是一样的,因为生成器依赖于系统的事件,方便为ID排序
    甚至可以添加前缀
        // 前缀 
        echo uniqid('foo_'); 
        /* 输出 
        foo_4bd67d6cd8b8f 
        */ 

     

      // 有更多的熵

        echo uniqid('',true);

        /* 输出

        4bd67d6cd8b926.12135106

        */ 

    7.序列化

    序列化serialize()和反序列化 unserialize()
    其实json_encode()和json_decode()可以实现类似的功能

    8.字符串压缩

    gzcompress() 和 gzuncompress()

    9.注册停止函数

    当我们的脚本执行完成或意外死掉导致PHP执行即将关闭时,我们的这个函数将会被调用
    <?php
    $clean = false;
    function shutdown_func(){
      global $clean;
      if (!$clean){
        die("not a clean shutdown");
      }
      return false;
    }
    register_shutdown_function("shutdown_func");
    $a = 1;
    $a = new FooClass(); // 将因为致命错误而失败,这时会触发注册的停止函数,因为是存储在内存中的,所以没有相对路径可言
    $clean = true;
    ?>
  • 相关阅读:
    C++ 将对象写入文件 并读取
    IronPython fail to add reference to WebDriver.dll
    How to Capture and Decrypt Lync Server 2010 TLS Traffic Using Microsoft Tools
    .net code injection
    数学系学生应该知道的十个学术网站
    Difference Between Currency Swap and FX Swap
    Swift开源parser
    谈谈我对证券公司一些部门的理解(前、中、后台)[z]
    JDK8记FullGC时候Metaspace内存不会被垃圾回收
    JVM源码分析之JDK8下的僵尸(无法回收)类加载器[z]
  • 原文地址:https://www.cnblogs.com/isuben/p/5570494.html
Copyright © 2011-2022 走看看