php功能-PHP开发必备的8个功能

2023-08-26 0 6,369 百度已收录

开发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: helloarg2: 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: helloarg2: worldarg3: again*/

2.使用glob()查找文件

大多数PHP函数的函数名从字面上就能明白它们的用途,但是当你听到glob()时,你可能不知道它是做什么用的,虽然glob()和scandir()类似,它都可以用来查找文件,请请参阅下面的用法

// 取得所有的后缀为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.txt13.)*/

您还可以添加路径:

$files =glob(‘../images/a*.jpg');print_r($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]);}echoFinal: “.memory_get_usage().” bytes n”;/* printsFinal: 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 directoriesrequire_once(‘config/database.php');// this is always relative to this file's path// no matter where it was included fromrequire_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 stringecho uniqid();/* 输出4bd67c947233e*/// generate another unique stringecho 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$string ="Lorem ipsum dolor sit amet, consecteturadipiscing elit. Nunc ut elit id mi ultriciesadipiscing. Nulla facilisi. Praesent pulvinar,sapien vel feugiat vestibulum, nulla dui pretium orci,non ultricies elit lacus quis ante. Lorem ipsum dolorsit amet, consectetur adipiscing elit. Aliquampretium ullamcorper urna quis iaculis. Etiam ac massased turpis tempor luctus. Curabitur sed nibh eu elitmollis congue. Praesent ipsum diam, consectetur vitaeornare a, aliquam a nunc. In id magna pellentesquetellus posuere adipiscing. Sed non mi metus, at laciniaaugue. Sed magna nisi, ornare in mollis in, mollissed nunc. Etiam at justo in leo congue mollis.Nullam in neque eget metus hendrerit scelerisqueeu non enim. Ut malesuada lacus eu nulla bibendumid euismod urna sodales. ";$compressed = gzcompress($string);echo “Original size: “.strlen($string).”n”;//  输出原始大小Original size: 800echo “Compressed size: “.strlen($compressed).”n”;//  输出压缩后的大小Compressed size: 418// 解压缩$original = gzuncompress($compressed);

看完本文有收获?点赞、分享是最大的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悟空资源网 php php功能-PHP开发必备的8个功能 https://www.wkzy.net/game/160247.html

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务