8 个必备的PHP功能开发

1、传递任意数量的函数参数 

// 两个默认参数的函数  
function foo($arg1 = ”, $arg2 = ”) {  
echo “arg1: $arg1\n”;  
echo “arg2: $arg2\n”;  
}  
foo(‘hello’,'world’);  
/* 输出: 
arg1: hello 
arg2: world 
*/  
foo();  
/* 输出: 
arg1: 
arg2: 
*/  
下面这个示例是PHP的不定参数用法,其使用到了 func_get_args()方法:  
// 是的,形参列表为空  
function foo() {  
// 取得所有的传入参数的数组  
$args = func_get_args();  
foreach ($args as $k => $v) {  
echo “arg”.($k+1).”: $v\n”;  
}  
}  
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 
) 
*/ 
$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:\wamp\www\images\apple.jpg 
[1] => C:\wamp\www\images\art.jpg 
) 
*/  

3.生成唯一的id 

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

// generate unique string  
echo uniqid();  
/* 输出 
4bd67c947233e 
*/  
// generate another unique string  
echo uniqid();  
/* 输出 
4bd67c9472340 
*/  

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

// 前缀  
echo uniqid(‘foo_’);  
/* 输出 
foo_4bd67d6cd8b8f 
*/  
// 有更多的熵  
echo uniqid(”,true);  
/* 输出 
4bd67d6cd8b926.12135106 
*/  
// 都有  
echo uniqid(‘bar_’,true);  
/* 输出 
bar_4bd67da367b650.43684647 
*/  

  

 

4.序列化 

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

// 一个复杂的数组  
$myvar = array(  
‘hello’,  
42,  
array(1,’two’),  
‘apple’  
);  
// 序列化  
$string = serialize($myvar);  
echo $string;  
/* 输出 
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”;} 
*/  
// 反序例化  
$newvar = unserialize($string);  
print_r($newvar);  
/* 输出 
Array 
( 
[0] => hello 
[1] => 42 
[2] => Array 
( 
[0] => 1 
[1] => two 
) 
[3] => apple 
) 
*/  

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

// a complex array  
$myvar = array(  
‘hello’,  
42,  
array(1,’two’),  
‘apple’  
);  
// convert to a string  
$string = json_encode($myvar);  
echo $string;  
/* prints 
["hello",42,[1,"two"],”apple”] 
*/  
// 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 
) 
*/  

  

 

posted @ 2013-10-28 17:35  冯志强采桑子  阅读(262)  评论(0编辑  收藏  举报