开发PHP的程序员应该知道,PHP中有很多外部函数。 掌握它们可以帮助你在进行PHP开发时变得更加得心应手。 本文将分享8个开发必备的PHP函数php功能,都很实用,希望各位PHP开发者能够掌握。
1.传递任意数量的函数参数
在我们的.NET或JAVA编程中,函数参数的数量通常是固定的,而PHP允许你使用任意数量的参数。 以下示例显示了 PHP 函数的默认参数:
// 两个默认参数的函数
function foo($arg1 = ”,$arg2 = ”) {
echo “arg1:$arg1n”;
echo “arg2:$arg2n”;
}
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).”:$vn”;
}
}
foo();
/* 什么也不会输出 */
foo(‘hello');
/* 输出
arg1: hello
*/
foo(‘hello', ‘world', ‘again');
/* 输出
arg1: hello
arg2: world
arg3: again
*/
2.使用glob()查找文件
大多数PHP函数的函数名从字面上就能明白它们的用途,但是当你听到glob()时,你可能不知道它是做什么用的,虽然glob()和scandir()类似,它都可以用来查找文件,请请参阅下面的用法:
// 取得所有的后缀为PHP的文件
'); files =glob(‘*.php
files);
/* 输出:
Array
(
[0] => phptest.php
[1] => pi.php
[2] => post_output.php
[3] => test.php
)
*/
你还可以查找多种后缀名:
// 取PHP文件和TXT文件
files =glob(‘*.{php,txt}', GLOB_BRACE);
files);
/* 输出:
Array
(
[0] => phptest.php
[1] => pi.php
[2] => post_output.php
[3] => test.php
[4] => log.txt
[5] => test.txt
13.
)
*/
您还可以添加路径:
'); files =glob(‘../images/a*.jpg
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.获取显存使用信息
PHP的显存回收机制已经很强大了。 还可以使用PHP脚本获取当前显存使用情况,调用memory_get_usage()函数获取当前显存使用情况,调用memory_get_peak_usage()函数获取显存使用峰值。 参考代码如下:
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
*/
4.获取CPU使用信息
获取显存使用率后,还可以使用PHP的getrusage()来获取CPU使用率,Windows下无法获取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
)
*/
5. 获取系统常量
PHP 提供了特别有用的系统常量,允许您获取当前行号 (__LINE__)、文件 (__FILE__)、目录 (__DIR__)、函数名称 (__FUNCTION__)、类名称 (__CLASS__)、技术名称 (__METHOD__) 和子空间 (__NAMESPACE__) ),很像C语言。
我们可以认为这种东西主要是用来调试的php功能,但事实却不一定。 比如我们在包含其他文件的时候可以使用?__FILE__(其实PHP5.3之后也可以使用__DIR__),下面是一个反例。
// 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');
下面是使用__LINE__输出一些调试信息,帮助你调试程序:
// 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”;
}
6.生成唯一id
很多同学使用md5()来创建下辈子的唯一编号,而md5()有几个缺点: 1.乱序,导致数据库排序性能下降。 2、太长,需要更多的存储空间。 虽然PHP自带了一个在来世创建唯一id的函数,但这个函数就是uniqid()。 下面是用法:
// generate unique string
echo uniqid();
/* 输出
4bd67c947233e
*/
// generate another unique string
echo uniqid();
/* 输出
4bd67c9472340
*/
7. 序列化
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
)
*/
8. 字符串压缩
当我们谈论压缩时,我们可能会想到文件压缩,当然字符串也可以被压缩。 PHP 提供了 gzcompress() 和 gzuncompress() 函数:
<?php
=
ipsum dolor sit amet, consectetur
adipiscing elit. Nunc ut elit id mi ultricies
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
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. ";
gzcompress($string); =
echo “Original size: “.strlen($string).”n”;
输出原始大小
Original size: 800
echo “Compressed size: “.strlen($compressed).”n”;
输出压缩后的大小
Compressed size: 418
解压缩
gzuncompress($compressed); =
看完本文有收获?点赞、分享是最大的支持!