8个必备的PHP功能开发
《:8个必备的PHP功能开发》要点: PHP教程PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以赞助你在做PHP开发时更加得心应手,本文将分享8个开发必备的PHP功能,个个都非常实用,希望各位PHP开发者能够掌握.?? PHP教程1、传递任意数量的函数参数? PHP教程
// 两个默认参数的函数
function foo($arg1 = ”,$arg2 = ”) {
echo “arg1: $arg1n”;
echo “arg2: $arg2n”;
}
foo(‘hello','world');
/* 输出:
arg1: hello
arg2: world
*/
foo();
/* 输出:
arg1:
arg2:
*/
PHP教程下面这个示例是PHP的不定参数用法,其使用到了 func_get_args()办法: PHP教程
// 是的,形参列表为空
function foo() {
// 取得所有的传入参数的数组
$args = func_get_args();
foreach ($args as $k => $v) {
echo “arg”.($k+1).”: $vn”;
}
}
foo();
/* 什么也不会输出 */
foo(‘hello');
/* 输出
arg1: hello
*/
foo(‘hello',‘world',‘again');
/* 输出
arg1: hello
arg2: world
arg3: again
*/
PHP教程2、使用glob()查找文件 PHP教程
// 取得所有的后缀为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
13.
)
*/
PHP教程你还可以加上路径: PHP教程
$files = glob(‘../images/a*.jpg');
print_r($files);
/* 输出:
Array
(
[0] => ../images/apple.jpg
[1] => ../images/art.jpg
)
*/
PHP教程如果你想得到绝对路径,你可以调用 realpath() 函数: PHP教程
$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
)
*/
PHP教程3、获取内存使用情况信息? PHP教程
echo “Initial: “.memory_get_usage().” bytes n”;
/* 输出
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 n”;
/* prints
Final: 885912 bytes
*/
echo “Peak: “.memory_get_peak_usage().” bytes n”;
/* 输出峰值
Peak: 13687072 bytes
*/
PHP教程4、获取CPU使用情况信息?? PHP教程
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
)
*/
PHP教程这个结构看上出很晦涩,除非你对CPU很了解.下面一些解释: PHP教程
// 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
*/
PHP教程这花了大约14秒的CPU时间,几乎所有的都是用户的时间,因为没有系统调用. PHP教程
$start = microtime(true);
// keep calling microtime for about 3 seconds
while(microtime(true) C $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
*/
PHP教程我们可以看到上面这个例子更耗CPU.? PHP教程
// 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');
PHP教程?下面是使用 __LINE__ 来输出一些debug的信息,这样有助于你调试程序: PHP教程
// some code
// …
my_debug(“some debug message”,__LINE__);
/* 输出
Line 4: some debug message
*/
// some more code
// …
my_debug(“another debug message”,__LINE__);
/* 输出
Line 11: another debug message
*/
function my_debug($msg,$line) {
echo “Line $line: $msgn”;
}
PHP教程6、生成唯一的id? PHP教程
// generate unique string
echo uniqid();
/* 输出
4bd67c947233e
*/
// generate another unique string
echo uniqid();
/* 输出
4bd67c9472340
*/
PHP教程该算法是根据CPU时间戳来生成的,所以在相近的时间段内,id前几位是一样的,这也便利id的排序,如果你想更好的避免重复,可以在id前加上前缀,如: PHP教程
// 前缀
echo uniqid(‘foo_');
/* 输出
foo_4bd67d6cd8b8f
*/
// 有更多的熵
echo uniqid(”,true);
/* 输出
4bd67d6cd8b926.12135106
*/
// 都有
echo uniqid(‘bar_',true);
/* 输出
bar_4bd67da367b650.43684647
*/
PHP教程7、序列化? PHP教程
// 一个复杂的数组
$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
)
*/
PHP教程如何序列化成json格式呢,放心,php也已经为你做好了,使用php 5.2以上版本的用户可以使用json_encode() 和 json_decode() 函数来实现json格式的序列化,代码如下: PHP教程
// a complex array
$myvar = array(
‘hello',‘apple'
);
// convert to a string
$string = json_encode($myvar);
echo $string;
/* prints
["hello",[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
)
*/
PHP教程8、字符串压缩? PHP教程
$string =
“Lorem ipsum dolor sit amet,consectetur
adipiscing elit. Nunc ut elit id mi ultricies
adipiscing. Nulla facilisi. Praesent pulvinar,sapien vel feugiat vestibulum,nulla dui pretium orci,non ultricies elit lacus quis ante. Lorem ipsum dolor
sit amet,consectetur adipiscing elit. Aliquam
pretium ullamcorper urna quis iaculis. Etiam ac massa
sed turpis tempor luctus. Curabitur sed nibh eu elit
mollis congue. Praesent ipsum diam,consectetur vitae
ornare a,aliquam a nunc. In id magna pellentesque
tellus posuere adipiscing. Sed non mi metus,at lacinia
augue. Sed magna nisi,ornare in mollis in,mollis
sed nunc. Etiam at justo in leo congue mollis.
Nullam in neque eget metus hendrerit scelerisque
eu non enim. Ut malesuada lacus eu nulla bibendum
id euismod urna sodales. “;
$compressed = gzcompress($string);
echo “Original size: “. strlen($string).”n”;
/* 输出原始大小
Original size: 800
*/
echo “Compressed size: “. strlen($compressed).”n”;
/* 输出压缩后的大小
Compressed size: 418
*/
// 解压缩
$original = gzuncompress($compressed);
PHP教程几乎有50% 压缩比率.同时,你还可以使用 gzencode() 和 gzdecode() 函数来压缩,只不用其用了不同的压缩算法.?? 欢迎参与《:8个必备的PHP功能开发》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |