Programmers who have done PHP development should know that there are many built-in functions in PHP. Mastering them can help you become more comfortable in PHP development. This article will share 8 essential PHP functions for development, each of which They are all very practical and I hope all PHP developers can master them.
1. Pass any number of function parameters
2. Use glob() to find files
3. Get memory usage information
4. Get CPU usage information
5. Get system constants
6. Generate unique id
7. Serialization
8. String compression
1. Pass any number of function parameters In .NET or JAVA programming, the number of function parameters is generally fixed, but PHP allows you to use any number of parameters. The following example shows you the default parameters of a PHP function:
<span 1</span> <span //</span><span 两个默认参数的函数 </span> <span 2</span> <span function</span> foo(<span $arg1</span> = ", <span $arg2</span> =<span ") { </span><span 3</span> <span echo</span> "arg1: <span $arg1</span><span \n"; </span><span 4</span> <span echo</span> "arg2: <span $arg2</span><span \n"; </span><span 5</span> <span } </span><span 6</span> foo('hello','<span world'); </span><span 7</span> <span /* 输出: </span><span 8</span> <span arg1: hello </span><span 9</span> <span arg2: world </span><span 10</span> <span */ </span><span 11</span> <span foo(); </span><span 12</span> <span /* 输出: </span><span 13</span> <span arg1: </span><span 14</span> <span arg2: </span><span 15</span> <span */ </span><span 16</span> <span 下面这个示例是PHP的不定参数用法,其使用到了 func_get_args()方法: </span><span 17</span> <span // 是的,形参列表为空 </span><span 18</span> <span function foo() { </span><span 19</span> <span // 取得所有的传入参数的数组 </span><span 20</span> <span $args = func_get_args(); </span><span 21</span> <span foreach ($args as $k => $v) { </span><span 22</span> <span echo "arg".($k+1).": $v\n"; </span><span 23</span> <span } </span><span 24</span> <span } </span><span 25</span> <span foo(); </span><span 26</span> <span /* 什么也不会输出 */ </span><span 27</span> <span foo('hello'); </span><span 28</span> <span /* 输出 </span><span 29</span> <span arg1: hello </span><span 30</span> <span */ </span><span 31</span> <span foo('hello', 'world', 'again'); </span><span 32</span> <span /* 输出 </span><span 33</span> <span arg1: hello </span><span 34</span> <span arg2: world </span><span 35</span> <span arg3: again </span><span 36</span> */
2. Use glob() to find files The function names of most PHP functions can literally understand their purpose, but when you see glob(), you may not know this. What is it used for? In fact, glob() is the same as scandir(). It can be used to find files. Please see the usage below:
<span 1</span> <span //</span><span 取得所有的后缀为PHP的文件 </span> <span 2</span> <span $files</span> = <span glob</span>('*.<span php'); </span><span 3</span> <span print_r</span>(<span $files</span><span ); </span><span 4</span> <span /*</span><span 输出: </span><span 5</span> <span Array </span><span 6</span> <span ( </span><span 7</span> <span [0] => phptest.php </span><span 8</span> <span [1] => pi.php </span><span 9</span> <span [2] => post_output.php </span><span 10</span> <span [3] => test.php </span><span 11</span> <span ) </span><span 12</span> <span */</span>
You can also search for multiple suffixes:
<span 1</span> <span //</span><span 取PHP文件和TXT文件 </span> <span 2</span> <span $files</span> = <span glob</span>('*.{php,txt}',<span GLOB_BRACE); </span><span 3</span> <span print_r</span>(<span $files</span><span ); </span><span 4</span> <span /*</span><span 输出: </span><span 5</span> <span Array </span><span 6</span> <span ( </span><span 7</span> <span [0] => phptest.php </span><span 8</span> <span [1] => pi.php </span><span 9</span> <span [2] => post_output.php </span><span 10</span> <span [3] => test.php </span><span 11</span> <span [4] => log.txt </span><span 12</span> <span [5] => test.txt </span><span 13</span> <span ) </span><span 14</span> <span */</span>
You can also add the path:
<span 1</span> <span $files</span> = <span glob</span>('../images/a*.<span jpg'); </span><span 2</span> <span print_r</span>(<span $files</span><span ); </span><span 3</span> <span /*</span><span 输出: </span><span 4</span> <span Array </span><span 5</span> <span ( </span><span 6</span> <span [0] => ../images/apple.jpg </span><span 7</span> <span [1] => ../images/art.jpg </span><span 8</span> <span ) </span><span 9</span> <span */</span>
If you want to get the absolute path, you can call the realpath() function:
<span 1</span> <span $files</span> = <span glob</span>('../images/a*.<span jpg'); </span><span 2</span> <span //</span><span applies the function to each array element </span> <span 3</span> <span $files</span> = <span array_map</span>('<span realpath</span>',<span $files</span><span ); </span><span 4</span> <span print_r</span>(<span $files</span><span ); </span><span 5</span> <span /*</span><span output looks like: </span><span 6</span> <span Array </span><span 7</span> <span ( </span><span 8</span> <span [0] => C:\wamp\www\images\apple.jpg </span><span 9</span> <span [1] => C:\wamp\www\images\art.jpg </span><span 10</span> <span ) </span><span 11</span> <span */</span>
3、获取内存使用情况信息 PHP的内存回收机制已经非常强大,你也可以使用PHP脚本获取当前内存的使用情况,调用memory_get_usage() 函数获取当期内存使用情况,调用memory_get_peak_usage() 函数获取内存使用的峰值。参考代码如下:
<span 1</span> <span echo</span> "Initial: ".memory_get_usage().<span " bytes \n"; </span><span 2</span> <span /*</span><span 输出 </span><span 3</span> <span Initial: 361400 bytes </span><span 4</span> <span */</span> <span 5</span> <span //</span><span 使用内存 </span> <span 6</span> <span for</span> (<span $i</span> = 0; <span $i</span> < 100000; <span $i</span>++<span ) { </span><span 7</span> <span $array</span> []= <span md5</span>(<span $i</span><span ); </span><span 8</span> <span } </span><span 9</span> <span //</span><span 删除一半的内存 </span> <span 10</span> <span for</span> (<span $i</span> = 0; <span $i</span> < 100000; <span $i</span>++<span ) { </span><span 11</span> <span unset</span>(<span $array</span>[<span $i</span><span ]); </span><span 12</span> <span } </span><span 13</span> <span echo</span> "<span Final</span>: ".memory_get_usage().<span " bytes \n"; </span><span 14</span> <span /*</span><span prints </span><span 15</span> <span Final: 885912 bytes </span><span 16</span> <span */</span> <span 17</span> <span echo</span> "Peak: ".memory_get_peak_usage().<span " bytes \n"; </span><span 18</span> <span /*</span><span 输出峰值 </span><span 19</span> <span Peak: 13687072 bytes </span><span 20</span> <span */</span>
4、获取CPU使用情况信息 获取了内存使用情况,也可以使用PHP的 getrusage()获取CPU使用情况,该方法在windows下不可用。
<span 1</span> <span print_r</span><span (getrusage()); </span><span 2</span> <span /*</span><span 输出 </span><span 3</span> <span Array </span><span 4</span> <span ( </span><span 5</span> <span [ru_oublock] => 0 </span><span 6</span> <span [ru_inblock] => 0 </span><span 7</span> <span [ru_msgsnd] => 2 </span><span 8</span> <span [ru_msgrcv] => 3 </span><span 9</span> <span [ru_maxrss] => 12692 </span><span 10</span> <span [ru_ixrss] => 764 </span><span 11</span> <span [ru_idrss] => 3864 </span><span 12</span> <span [ru_minflt] => 94 </span><span 13</span> <span [ru_majflt] => 0 </span><span 14</span> <span [ru_nsignals] => 1 </span><span 15</span> <span [ru_nvcsw] => 67 </span><span 16</span> <span [ru_nivcsw] => 4 </span><span 17</span> <span [ru_nswap] => 0 </span><span 18</span> <span [ru_utime.tv_usec] => 0 </span><span 19</span> <span [ru_utime.tv_sec] => 0 </span><span 20</span> <span [ru_stime.tv_usec] => 6269 </span><span 21</span> <span [ru_stime.tv_sec] => 0 </span><span 22</span> <span ) </span><span 23</span> <span */</span>
这个结构看上出很晦涩,除非你对CPU很了解。下面一些解释: ru_oublock: 块输出操作 ru_inblock: 块输入操作 ru_msgsnd: 发送的message ru_msgrcv: 收到的message ru_maxrss: 最大驻留集大小 ru_ixrss: 全部共享内存大小 ru_idrss:全部非共享内存大小 ru_minflt: 页回收 ru_majflt: 页失效 ru_nsignals: 收到的信号 ru_nvcsw: 主动上下文切换 ru_nivcsw: 被动上下文切换 ru_nswap: 交换区 ru_utime.tv_usec: 用户态时间 (microseconds) ru_utime.tv_sec: 用户态时间(seconds) ru_stime.tv_usec: 系统内核时间 (microseconds) ru_stime.tv_sec: 系统内核时间?(seconds) 要看到你的脚本消耗了多少CPU,我们需要看看"用户态的时间"和"系统内核时间"的值。秒和微秒部分是分别提供的,您可以把微秒值除以100万,并把它添加到秒的值后,可以得到有小数部分的秒数。
<span 1</span> <span //</span><span sleep for 3 seconds (non-busy) </span> <span 2</span> <span sleep</span>(3<span ); </span><span 3</span> <span $data</span> =<span getrusage(); </span><span 4</span> <span echo</span> "User <span time</span>: ". <span 5</span> (<span $data</span>['ru_utime.tv_sec'] + <span 6</span> <span $data</span>['ru_utime.tv_usec'] / 1000000<span ); </span><span 7</span> <span echo</span> "<span System</span> <span time</span>: ". <span 8</span> (<span $data</span>['ru_stime.tv_sec'] + <span 9</span> <span $data</span>['ru_stime.tv_usec'] / 1000000<span ); </span><span 10</span> <span /*</span><span 输出 </span><span 11</span> <span User time: 0.011552 </span><span 12</span> <span System time: 0 </span><span 13</span> <span */</span>
是不占用系统时间的,我们可以来看下面的一个例子:
<span 1</span> <span //</span><span loop 10 million times (busy) </span> <span 2</span> <span for</span>(<span $i</span>=0;<span $i</span><10000000;<span $i</span>++<span ) { </span><span 3</span> <span } </span><span 4</span> <span $data</span> =<span getrusage(); </span><span 5</span> <span echo</span> "User <span time</span>: ". <span 6</span> (<span $data</span>['ru_utime.tv_sec'] + <span 7</span> <span $data</span>['ru_utime.tv_usec'] / 1000000<span ); </span><span 8</span> <span echo</span> "<span System</span> <span time</span>: ". <span 9</span> (<span $data</span>['ru_stime.tv_sec'] + <span 10</span> <span $data</span>['ru_stime.tv_usec'] / 1000000<span ); </span><span 11</span> <span /*</span><span 输出 </span><span 12</span> <span User time: 1.424592 </span><span 13</span> <span System time: 0.004204 </span><span 14</span> <span */</span>
这花了大约14秒的CPU时间,几乎所有的都是用户的时间,因为没有系统调用。传统时间是CPU花费在系统调用上的上执行内核指令的时间。下面是一个例子:
<span 1</span> <span $start</span> = <span microtime</span>(<span true</span><span ); </span><span 2</span> <span //</span><span keep calling microtime for about 3 seconds </span> <span 3</span> <span while</span>(<span microtime</span>(<span true</span>) – <span $start</span> < 3<span ) { </span><span 4</span> <span } </span><span 5</span> <span $data</span> =<span getrusage(); </span><span 6</span> <span echo</span> "User <span time</span>: ". <span 7</span> (<span $data</span>['ru_utime.tv_sec'] + <span 8</span> <span $data</span>['ru_utime.tv_usec'] / 1000000<span ); </span><span 9</span> <span echo</span> "<span System</span> <span time</span>: ". <span 10</span> (<span $data</span>['ru_stime.tv_sec'] + <span 11</span> <span $data</span>['ru_stime.tv_usec'] / 1000000<span ); </span><span 12</span> <span /*</span><span prints </span><span 13</span> <span User time: 1.088171 </span><span 14</span> <span System time: 1.675315 </span><span 15</span> <span */</span>
我们可以看到上面这个例子更耗CPU。
5、获取系统常量 PHP 提供非常有用的系统常量 可以让你得到当前的行号 (__LINE__),文件 (__FILE__),目录 (__DIR__),函数名 (__FUNCTION__),类名(__CLASS__),方法名(__METHOD__) 和名字空间 (__NAMESPACE__),很像C语言。
我们可以以为这些东西主要是用于调试,当也不一定,比如我们可以在include其它文件的时候使用?__FILE__ (当然,你也可以在 PHP 5.3以后使用 __DIR__ ),下面是一个例子。
<span 1</span> <span //</span><span this is relative to the loaded script's path </span><span 2</span> <span // it may cause problems when running scripts from different directories </span> <span 3</span> <span require_once</span>('config/database.<span php'); </span><span 4</span> <span //</span><span this is always relative to this file's path </span><span 5</span> <span // no matter where it was included from </span> <span 6</span> <span require_once</span>(<span dirname</span>(<span __FILE__</span>) . '/config/database.php');
下面是使用 __LINE__ 来输出一些debug的信息,这样有助于你调试程序:
<span 1</span> <span //</span><span some code </span><span 2</span> <span // … </span> <span 3</span> my_debug("some debug message", <span __LINE__</span><span ); </span><span 4</span> <span /*</span><span 输出 </span><span 5</span> <span Line 4: some debug message </span><span 6</span> <span */</span> <span 7</span> <span //</span><span some more code </span><span 8</span> <span // … </span> <span 9</span> my_debug("another debug message", <span __LINE__</span><span ); </span><span 10</span> <span /*</span><span 输出 </span><span 11</span> <span Line 11: another debug message </span><span 12</span> <span */</span> <span 13</span> <span function</span> my_debug(<span $msg</span>, <span $line</span><span ) { </span><span 14</span> <span echo</span> "Line <span $line</span>: <span $msg</span><span \n"; </span><span 15</span> }
6、生成唯一的id 很多朋友都利用md5()来生成唯一的编号,但是md5()有几个缺点:1、无序,导致数据库中排序性能下降。2、太长,需要更多的存储空间。其实PHP中自带一个函数来生成唯一的id,这个函数就是uniqid()。下面是用法:
<span 1</span> <span //</span><span generate unique string </span> <span 2</span> <span echo</span> <span uniqid</span><span (); </span><span 3</span> <span /*</span><span 输出 </span><span 4</span> <span 4bd67c947233e </span><span 5</span> <span */</span> <span 6</span> <span //</span><span generate another unique string </span> <span 7</span> <span echo</span> <span uniqid</span><span (); </span><span 8</span> <span /*</span><span 输出 </span><span 9</span> <span 4bd67c9472340 </span><span 10</span> <span */</span>
该算法是根据CPU时间戳来生成的,所以在相近的时间段内,id前几位是一样的,这也方便id的排序,如果你想更好的避免重复,可以在id前加上前缀,如:
<span 1</span> <span //</span><span 前缀 </span> <span 2</span> <span echo</span> <span uniqid</span><span ('foo_'); </span><span 3</span> <span /*</span><span 输出 </span><span 4</span> <span foo_4bd67d6cd8b8f </span><span 5</span> <span */</span> <span 6</span> <span //</span><span 有更多的熵 </span> <span 7</span> <span echo</span> <span uniqid</span>(",<span true</span><span ); </span><span 8</span> <span /*</span><span 输出 </span><span 9</span> <span 4bd67d6cd8b926.12135106 </span><span 10</span> <span */</span> <span 11</span> <span //</span><span 都有 </span> <span 12</span> <span echo</span> <span uniqid</span>('bar_',<span true</span><span ); </span><span 13</span> <span /*</span><span 输出 </span><span 14</span> <span bar_4bd67da367b650.43684647 </span><span 15</span> <span */</span>
7、序列化 PHP序列化功能大家可能用的比较多,也比较常见,当你需要把数据存到数据库或者文件中是,你可以利用PHP中的serialize() 和 unserialize()方法来实现序列化和反序列化,代码如下:
<span 1</span> <span //</span><span 一个复杂的数组 </span> <span 2</span> <span $myvar</span> = <span array</span><span ( </span><span 3</span> 'hello', <span 4</span> 42, <span 5</span> <span array</span>(1,'two'), <span 6</span> <span 'apple' </span><span 7</span> <span ); </span><span 8</span> <span //</span><span 序列化 </span> <span 9</span> <span $string</span> = <span serialize</span>(<span $myvar</span><span ); </span><span 10</span> <span echo</span> <span $string</span><span ; </span><span 11</span> <span /*</span><span 输出 </span><span 12</span> <span 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";} </span><span 13</span> <span */</span> <span 14</span> <span //</span><span 反序例化 </span> <span 15</span> <span $newvar</span> = <span unserialize</span>(<span $string</span><span ); </span><span 16</span> <span print_r</span>(<span $newvar</span><span ); </span><span 17</span> <span /*</span><span 输出 </span><span 18</span> <span Array </span><span 19</span> <span ( </span><span 20</span> <span [0] => hello </span><span 21</span> <span [1] => 42 </span><span 22</span> <span [2] => Array </span><span 23</span> <span ( </span><span 24</span> <span [0] => 1 </span><span 25</span> <span [1] => two </span><span 26</span> <span ) </span><span 27</span> <span [3] => apple </span><span 28</span> <span ) </span><span 29</span> <span */</span>
如何序列化成json格式呢,放心,php也已经为你做好了,使用php 5.2以上版本的用户可以使用json_encode() 和 json_decode() 函数来实现json格式的序列化,代码如下:
<span 1</span> <span //</span><span a complex array </span> <span 2</span> <span $myvar</span> = <span array</span><span ( </span><span 3</span> 'hello', <span 4</span> 42, <span 5</span> <span array</span>(1,'two'), <span 6</span> <span 'apple' </span><span 7</span> <span ); </span><span 8</span> <span //</span><span convert to a string </span> <span 9</span> <span $string</span> = json_encode(<span $myvar</span><span ); </span><span 10</span> <span echo</span> <span $string</span><span ; </span><span 11</span> <span /*</span><span prints </span><span 12</span> <span ["hello",42,[1,"two"],"apple"] </span><span 13</span> <span */</span> <span 14</span> <span //</span><span you can reproduce the original variable </span> <span 15</span> <span $newvar</span> = json_decode(<span $string</span><span ); </span><span 16</span> <span print_r</span>(<span $newvar</span><span ); </span><span 17</span> <span /*</span><span prints </span><span 18</span> <span Array </span><span 19</span> <span ( </span><span 20</span> <span [0] => hello </span><span 21</span> <span [1] => 42 </span><span 22</span> <span [2] => Array </span><span 23</span> <span ( </span><span 24</span> <span [0] => 1 </span><span 25</span> <span [1] => two </span><span 26</span> <span ) </span><span 27</span> <span [3] => apple </span><span 28</span> <span ) </span><span 29</span> <span */</span>
8、字符串压缩 当我们说到压缩,我们可能会想到文件压缩,其实,字符串也是可以压缩的。PHP提供了 gzcompress() 和gzuncompress() 函数:
<span 1</span> <span $string</span> = <span 2</span> "Lorem ipsum dolor sit amet,<span consectetur </span><span 3</span> adipiscing elit.<span Nunc ut elit id mi ultricies </span><span 4</span> adipiscing. Nulla facilisi. Praesent pulvinar, <span 5</span> sapien vel feugiat vestibulum, nulla dui pretium orci, <span 6</span> non ultricies elit lacus quis ante.<span Lorem ipsum dolor </span><span 7</span> sit amet, consectetur adipiscing elit.<span Aliquam </span><span 8</span> pretium ullamcorper urna quis iaculis.<span Etiam ac massa </span><span 9</span> sed turpis tempor luctus.<span Curabitur sed nibh eu elit </span><span 10</span> mollis congue. Praesent ipsum diam,<span consectetur vitae </span><span 11</span> ornare a, aliquam a nunc.<span In id magna pellentesque </span><span 12</span> tellus posuere adipiscing. Sed non mi metus,<span at lacinia </span><span 13</span> augue. Sed magna nisi, ornare in mollis in,<span mollis </span><span 14</span> sed nunc. Etiam at justo in leo congue mollis. <span 15</span> <span Nullam in neque eget metus hendrerit scelerisque </span><span 16</span> eu non enim.<span Ut malesuada lacus eu nulla bibendum </span><span 17</span> id euismod urna sodales.<span "; </span><span 18</span> <span $compressed</span> = <span gzcompress</span>(<span $string</span><span ); </span><span 19</span> <span echo</span> "Original size: ". <span strlen</span>(<span $string</span>).<span "\n"; </span><span 20</span> <span /*</span><span 输出原始大小 </span><span 21</span> <span Original size: 800 </span><span 22</span> <span */</span> <span 23</span> <span echo</span> "Compressed size: ". <span strlen</span>(<span $compressed</span>).<span "\n"; </span><span 24</span> <span /*</span><span 输出压缩后的大小 </span><span 25</span> <span Compressed size: 418 </span><span 26</span> <span */</span> <span 27</span> <span //</span><span 解压缩 </span> <span 28</span> <span $original</span> = <span gzuncompress</span>(<span $compressed</span>);
几乎有50% 压缩比率。同时,你还可以使用 gzencode() 和 gzdecode() 函数来压缩,只不用其用了不同的压缩算法。
以上就是8个开发必备的PHP功能,是不是都很实用呢?
原文出处:http://www.codeceo.com/8-php-functions.html