zoukankan      html  css  js  c++  java
  • 8个必备的PHP功能开发

    做过PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以帮助你在做PHP开发时更加得心应手,本文将分享8个开发必备的PHP功能,个个都非常实用,希望各位PHP开发者能够掌握。

    1、传递任意数量的函数参数 我们在.NET或者JAVA编程中,一般函数参数个数都是固定的,但是PHP允许你使用任意个数的参数。下面这个示例向你展示了PHP函数的默认参数:
     1 // 两个默认参数的函数  
     2 function foo($arg1 = ", $arg2 = ") {  
     3 echo "arg1: $arg1
    ";  
     4 echo "arg2: $arg2
    ";  
     5 }  
     6 foo('hello','world');  
     7 /* 输出: 
     8 arg1: hello 
     9 arg2: world 
    10 */  
    11 foo();  
    12 /* 输出: 
    13 arg1: 
    14 arg2: 
    15 */  
    16 下面这个示例是PHP的不定参数用法,其使用到了 func_get_args()方法:  
    17 // 是的,形参列表为空  
    18 function foo() {  
    19 // 取得所有的传入参数的数组  
    20 $args = func_get_args();  
    21 foreach ($args as $k => $v) {  
    22 echo "arg".($k+1).": $v
    ";  
    23 }  
    24 }  
    25 foo();  
    26 /* 什么也不会输出 */  
    27 foo('hello');  
    28 /* 输出 
    29 arg1: hello 
    30 */  
    31 foo('hello', 'world', 'again');  
    32 /* 输出 
    33 arg1: hello 
    34 arg2: world 
    35 arg3: again 
    36 */  

    2、使用glob()查找文件 大部分PHP函数的函数名从字面上都可以理解其用途,但是当你看到 glob() 的时候,你也许并不知道这是用来做什么的,其实glob()和scandir() 一样,可以用来查找文件,请看下面的用法:

     1 // 取得所有的后缀为PHP的文件  
     2 $files = glob('*.php');  
     3 print_r($files);  
     4 /* 输出: 
     5 Array 
     6 ( 
     7 [0] => phptest.php 
     8 [1] => pi.php 
     9 [2] => post_output.php 
    10 [3] => test.php 
    11 ) 
    12 */  

    你还可以查找多种后缀名:

     1 // 取PHP文件和TXT文件  
     2 $files = glob('*.{php,txt}', GLOB_BRACE);  
     3 print_r($files);  
     4 /* 输出: 
     5 Array 
     6 ( 
     7 [0] => phptest.php 
     8 [1] => pi.php 
     9 [2] => post_output.php 
    10 [3] => test.php 
    11 [4] => log.txt 
    12 [5] => test.txt 
    13 ) 
    14 */

    你还可以加上路径:

    1 $files = glob('../images/a*.jpg');  
    2 print_r($files);  
    3 /* 输出: 
    4 Array 
    5 ( 
    6 [0] => ../images/apple.jpg 
    7 [1] => ../images/art.jpg 
    8 ) 
    9 */ 

    如果你想得到绝对路径,你可以调用 realpath() 函数:

     1 $files = glob('../images/a*.jpg');  
     2 // applies the function to each array element  
     3 $files = array_map('realpath',$files);  
     4 print_r($files);  
     5 /* output looks like: 
     6 Array 
     7 ( 
     8 [0] => C:wampwwwimagesapple.jpg 
     9 [1] => C:wampwwwimagesart.jpg 
    10 ) 
    11 */  

    3、获取内存使用情况信息 PHP的内存回收机制已经非常强大,你也可以使用PHP脚本获取当前内存的使用情况,调用memory_get_usage() 函数获取当期内存使用情况,调用memory_get_peak_usage() 函数获取内存使用的峰值。参考代码如下:

     1 echo "Initial: ".memory_get_usage()." bytes 
    ";  
     2 /* 输出 
     3 Initial: 361400 bytes 
     4 */  
     5 // 使用内存  
     6 for ($i = 0; $i < 100000; $i++) {  
     7 $array []= md5($i);  
     8 }  
     9 // 删除一半的内存  
    10 for ($i = 0; $i < 100000; $i++) {  
    11 unset($array[$i]);  
    12 }  
    13 echo "Final: ".memory_get_usage()." bytes 
    ";  
    14 /* prints 
    15 Final: 885912 bytes 
    16 */  
    17 echo "Peak: ".memory_get_peak_usage()." bytes 
    ";  
    18 /* 输出峰值 
    19 Peak: 13687072 bytes 
    20 */  

    4、获取CPU使用情况信息 获取了内存使用情况,也可以使用PHP的 getrusage()获取CPU使用情况,该方法在windows下不可用。

     1 print_r(getrusage());  
     2 /* 输出 
     3 Array 
     4 ( 
     5 [ru_oublock] => 0 
     6 [ru_inblock] => 0 
     7 [ru_msgsnd] => 2 
     8 [ru_msgrcv] => 3 
     9 [ru_maxrss] => 12692 
    10 [ru_ixrss] => 764 
    11 [ru_idrss] => 3864 
    12 [ru_minflt] => 94 
    13 [ru_majflt] => 0 
    14 [ru_nsignals] => 1 
    15 [ru_nvcsw] => 67 
    16 [ru_nivcsw] => 4 
    17 [ru_nswap] => 0 
    18 [ru_utime.tv_usec] => 0 
    19 [ru_utime.tv_sec] => 0 
    20 [ru_stime.tv_usec] => 6269 
    21 [ru_stime.tv_sec] => 0 
    22 ) 
    23 */  

    这个结构看上出很晦涩,除非你对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万,并把它添加到秒的值后,可以得到有小数部分的秒数。

     1 // sleep for 3 seconds (non-busy)  
     2 sleep(3);  
     3 $data = getrusage();  
     4 echo "User time: ".  
     5 ($data['ru_utime.tv_sec'] +  
     6 $data['ru_utime.tv_usec'] / 1000000);  
     7 echo "System time: ".  
     8 ($data['ru_stime.tv_sec'] +  
     9 $data['ru_stime.tv_usec'] / 1000000);  
    10 /* 输出 
    11 User time: 0.011552 
    12 System time: 0 
    13 */  

    是不占用系统时间的,我们可以来看下面的一个例子:

     1 // loop 10 million times (busy)  
     2 for($i=0;$i<10000000;$i++) {  
     3 }  
     4 $data = getrusage();  
     5 echo "User time: ".  
     6 ($data['ru_utime.tv_sec'] +  
     7 $data['ru_utime.tv_usec'] / 1000000);  
     8 echo "System time: ".  
     9 ($data['ru_stime.tv_sec'] +  
    10 $data['ru_stime.tv_usec'] / 1000000);  
    11 /* 输出 
    12 User time: 1.424592 
    13 System time: 0.004204 
    14 */  

    这花了大约14秒的CPU时间,几乎所有的都是用户的时间,因为没有系统调用。传统时间是CPU花费在系统调用上的上执行内核指令的时间。下面是一个例子:

     1 $start = microtime(true);  
     2 // keep calling microtime for about 3 seconds  
     3 while(microtime(true) – $start < 3) {  
     4 }  
     5 $data = getrusage();  
     6 echo "User time: ".  
     7 ($data['ru_utime.tv_sec'] +  
     8 $data['ru_utime.tv_usec'] / 1000000);  
     9 echo "System time: ".  
    10 ($data['ru_stime.tv_sec'] +  
    11 $data['ru_stime.tv_usec'] / 1000000);  
    12 /* prints 
    13 User time: 1.088171 
    14 System time: 1.675315 
    15 */  

    我们可以看到上面这个例子更耗CPU。
    5、获取系统常量 PHP 提供非常有用的系统常量 可以让你得到当前的行号 (__LINE__),文件 (__FILE__),目录 (__DIR__),函数名  (__FUNCTION__),类名(__CLASS__),方法名(__METHOD__) 和名字空间  (__NAMESPACE__),很像C语言。
    我们可以以为这些东西主要是用于调试,当也不一定,比如我们可以在include其它文件的时候使用?__FILE__ (当然,你也可以在 PHP 5.3以后使用 __DIR__ ),下面是一个例子。

    1 // this is relative to the loaded script's path  
    2 // it may cause problems when running scripts from different directories  
    3 require_once('config/database.php');  
    4 // this is always relative to this file's path  
    5 // no matter where it was included from  
    6 require_once(dirname(__FILE__) . '/config/database.php');  

    下面是使用 __LINE__ 来输出一些debug的信息,这样有助于你调试程序:

     1 // some code  
     2 // …  
     3 my_debug("some debug message", __LINE__);  
     4 /* 输出 
     5 Line 4: some debug message 
     6 */  
     7 // some more code  
     8 // …  
     9 my_debug("another debug message", __LINE__);  
    10 /* 输出 
    11 Line 11: another debug message 
    12 */  
    13 function my_debug($msg, $line) {  
    14 echo "Line $line: $msg
    ";  
    15 }  

    6、生成唯一的id 很多朋友都利用md5()来生成唯一的编号,但是md5()有几个缺点:1、无序,导致数据库中排序性能下降。2、太长,需要更多的存储空间。其实PHP中自带一个函数来生成唯一的id,这个函数就是uniqid()。下面是用法:

     1 // generate unique string  
     2 echo uniqid();  
     3 /* 输出 
     4 4bd67c947233e 
     5 */  
     6 // generate another unique string  
     7 echo uniqid();  
     8 /* 输出 
     9 4bd67c9472340 
    10 */  

    该算法是根据CPU时间戳来生成的,所以在相近的时间段内,id前几位是一样的,这也方便id的排序,如果你想更好的避免重复,可以在id前加上前缀,如:

     1 // 前缀  
     2 echo uniqid('foo_');  
     3 /* 输出 
     4 foo_4bd67d6cd8b8f 
     5 */  
     6 // 有更多的熵  
     7 echo uniqid(",true);  
     8 /* 输出 
     9 4bd67d6cd8b926.12135106 
    10 */  
    11 // 都有  
    12 echo uniqid('bar_',true);  
    13 /* 输出 
    14 bar_4bd67da367b650.43684647 
    15 */  

    7、序列化 PHP序列化功能大家可能用的比较多,也比较常见,当你需要把数据存到数据库或者文件中是,你可以利用PHP中的serialize() 和 unserialize()方法来实现序列化和反序列化,代码如下:

     1 // 一个复杂的数组  
     2 $myvar = array(  
     3 'hello',  
     4 42,  
     5 array(1,'two'),  
     6 'apple'  
     7 );  
     8 // 序列化  
     9 $string = serialize($myvar);  
    10 echo $string;  
    11 /* 输出 
    12 a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:"two";}i:3;s:5:"apple";} 
    13 */  
    14 // 反序例化  
    15 $newvar = unserialize($string);  
    16 print_r($newvar);  
    17 /* 输出 
    18 Array 
    19 ( 
    20 [0] => hello 
    21 [1] => 42 
    22 [2] => Array 
    23 ( 
    24 [0] => 1 
    25 [1] => two 
    26 ) 
    27 [3] => apple 
    28 ) 
    29 */  

    如何序列化成json格式呢,放心,php也已经为你做好了,使用php 5.2以上版本的用户可以使用json_encode() 和 json_decode() 函数来实现json格式的序列化,代码如下:

     1 // a complex array  
     2 $myvar = array(  
     3 'hello',  
     4 42,  
     5 array(1,'two'),  
     6 'apple'  
     7 );  
     8 // convert to a string  
     9 $string = json_encode($myvar);  
    10 echo $string;  
    11 /* prints 
    12 ["hello",42,[1,"two"],"apple"] 
    13 */  
    14 // you can reproduce the original variable  
    15 $newvar = json_decode($string);  
    16 print_r($newvar);  
    17 /* prints 
    18 Array 
    19 ( 
    20 [0] => hello 
    21 [1] => 42 
    22 [2] => Array 
    23 ( 
    24 [0] => 1 
    25 [1] => two 
    26 ) 
    27 [3] => apple 
    28 ) 
    29 */  

    8、字符串压缩 当我们说到压缩,我们可能会想到文件压缩,其实,字符串也是可以压缩的。PHP提供了 gzcompress() 和gzuncompress() 函数:

     1 $string =  
     2 "Lorem ipsum dolor sit amet, consectetur  
     3 adipiscing elit. Nunc ut elit id mi ultricies  
     4 adipiscing. Nulla facilisi. Praesent pulvinar,  
     5 sapien vel feugiat vestibulum, nulla dui pretium orci,  
     6 non ultricies elit lacus quis ante. Lorem ipsum dolor  
     7 sit amet, consectetur adipiscing elit. Aliquam  
     8 pretium ullamcorper urna quis iaculis. Etiam ac massa  
     9 sed turpis tempor luctus. Curabitur sed nibh eu elit  
    10 mollis congue. Praesent ipsum diam, consectetur vitae  
    11 ornare a, aliquam a nunc. In id magna pellentesque  
    12 tellus posuere adipiscing. Sed non mi metus, at lacinia  
    13 augue. Sed magna nisi, ornare in mollis in, mollis  
    14 sed nunc. Etiam at justo in leo congue mollis.  
    15 Nullam in neque eget metus hendrerit scelerisque  
    16 eu non enim. Ut malesuada lacus eu nulla bibendum  
    17 id euismod urna sodales. ";  
    18 $compressed = gzcompress($string);  
    19 echo "Original size: ". strlen($string)."
    ";  
    20 /* 输出原始大小 
    21 Original size: 800 
    22 */  
    23 echo "Compressed size: ". strlen($compressed)."
    ";  
    24 /* 输出压缩后的大小 
    25 Compressed size: 418 
    26 */  
    27 // 解压缩  
    28 $original = gzuncompress($compressed);  

    几乎有50% 压缩比率。同时,你还可以使用 gzencode() 和 gzdecode() 函数来压缩,只不用其用了不同的压缩算法。
    以上就是8个开发必备的PHP功能,是不是都很实用呢?
    原文出处:http://www.codeceo.com/8-php-functions.html

     
  • 相关阅读:
    寻找项目中顶级Vue对象 (一)
    vue文件中style标签的几个标识符
    vue中methods中的方法闭包缓存问题
    斐波那契数列实现
    ECMAScript 6 入门学习笔记(零)——开始
    Extjs6(三)——用extjs6.0写一个简单页面
    同域和不同域长啥样
    Extjs6组件——Form大家族成员介绍
    Extjs6官方文档译文——应用架构简介(MVC,MVVM)
    Extjs6(特别篇)——项目自带例子main.js拆分详解
  • 原文地址:https://www.cnblogs.com/shlhm/p/3216151.html
Copyright © 2011-2022 走看看