你所必须知道的PHP 9种非常有用的函数和特征!

本文介绍了PHP编程中的多种高级技巧,包括处理可变数量参数的方法、使用glob遍历指定类型的文件、监测内存及CPU使用情况、利用魔法常量提高代码可读性、生成唯一ID、序列化复杂数据结构以及字符串压缩等。

1.函数中带有任意个数的参数

可选参数例子

// function with 2 optional arguments

function foo($arg1 = '', $arg2 = '') {

    echo "arg1: $arg1\n";

    echo "arg2: $arg2\n";

}

foo('hello','world');

foo();

任意参数例子

// yes, the argument list can be empty

function foo() {

    // returns an array of all passed arguments

    $args = func_get_args();

    foreach ($args as $k => $v) {

        echo "arg".($k+1).": $v\n";

    }

}

foo();

foo('hello');

foo('hello', 'world', 'again');

这个方法的核心是调用了核心的方法func_get_args()

2.使用glob()去寻找文档

遍历文件所在目录的所有已.php为结尾的文件

// get all php files

$files = glob('*.php');

print_r($files);

同时遍历两种模式,需要提供额外的参数(模式参数)。

// get all php files AND txt files

$files = glob('*.{php,txt}', GLOB_BRACE);

print_r($files);

这个函数可以准确的返回一个路径,这个返回结果是根据你的请求。

$files = glob('../images/a*.jpg');

print_r($files);

当然你也可以直接请求一个绝对路径。

$file = glob('D:/develop/http/*.php');

print_r($file);

如果你使用相对路径的请求,但是想要返回一个绝对路径的结果的话,请使用realpath().

3.内存使用情况

memory_get_usage()

memory_get_peak_usage()

echo "Initial: ".memory_get_usage()." bytes \n";

// let's use up some memory

for ($i = 0; $i < 100000; $i++) {

    $array []= md5($i);

}

// let's remove half of the array

for ($i = 0; $i < 100000; $i++) {

    unset($array[$i]);

}

echo "Final: ".memory_get_usage()." bytes \n";

echo "Peak: ".memory_get_peak_usage()." bytes \n";

4.cpu使用情况

getrusage();

print_r(getrusage());

5.php重要特性之一魔法常量

__LINE__ :当前行数

__FILE__: 当前文件名

__DIR__: 当前目录路劲

__FUNCTION__:函数名

__CLASS__:类名

__METHOD__:方法名

__NAMESPACE__:命名空间

// this is relative to the loaded script's path

// it may cause problems when running scripts from different directories

require_once('config/database.php');

// this is always relative to this file's path

// no matter where it was included from

require_once(dirname(__FILE__) . '/config/database.php');

// some code

// ...

my_debug("some debug message", __LINE__);

// some more code

// ...

my_debug("another debug message", __LINE__);

function my_debug($msg, $line) {

    echo "Line $line: $msg\n";

}

6.获取唯一的ID

uniqid()

// generate unique string

echo uniqid();

// with prefix

echo uniqid('foo_');

// with more entropy

echo uniqid('',true);

// both

echo uniqid('bar_',true);

7.序列化

serialize() unserililze()

// a complex array

$myvar = array(

    'hello',

    42,

    array(1,'two'),

    'apple'

);

// convert to a string

$string = serialize($myvar);

echo $string;

// you can reproduce the original variable

$newvar = unserialize($string);

print_r($newvar);

或者使用

json_encode() json_decode()

// a complex array

$myvar = array(

    'hello',

    42,

    array(1,'two'),

    'apple'

);

// convert to a string

$string = json_encode($myvar);

echo $string;

// you can reproduce the original variable

$newvar = json_decode($string);

print_r($newvar);

/* prints

Array

(

    [0] => hello

    [1] => 42

    [2] => Array

        (

            [0] => 1

            [1] => two

        )

    [3] => apple

)

8.压缩字符串

gzcompress()

gzuncompress()

gzencode()

gzdecode()

9.register_shutdown_function()

为脚本结束的时候注册一个函数。

这里说一下,原文作者提到一个我一直都忽略的问题:我们一般测试脚本的运行效率的时候一般都会用两个microtime()的差值来判断。原文作者对此保持一个怀疑的态度,因为即使你在你的脚本前后加了两个microtime()来求差,但是这实际上并不是你的整个脚本完成的一个时间,或者只能代表两个microtime()之间的代码的运行时间,但是你的php就仅仅做了这些代码之间的事情吗?

create_function()

创建一个匿名函数。

args :传给匿名函数的参数

code:匿名函数的代码

建议还是去看原文,这博文仅仅是从我的evernote中截取出来的笔记而已。如有错误,请私信我,我将速度进行修改。


转载于:https://my.oschina.net/zerodeng/blog/226837

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值