php实用功能小记
1.任意数目的参数
- function fun(){
- $args = func_get_args();
- foreach ($args as $k => $v) {
- echo "arg".($k+1).":";
- var_dump($v);
- echo "<br>";
- }
- }
- fun();
- echo "<hr>";
- fun(11,'22',[22,33]);
- echo "<hr>";
2.glob()
glob()函数返回匹配指定模式的文件名或目录。
可以使用glob函数来查找文件,也可以实现目录的遍历。
遍历当前目录下所有php文件
- $files = glob('*.php');
- var_dump($files);
Array ( [0] => algorithm.php [1] => createDatabaseDoc.php [2] => createDatabaseDoc1.php [3] => index.php [4] => jsonToMarkDown.php )
遍历当前目录下所有php和html文件
- $files = glob('*.{php,html}', GLOB_BRACE);
- print_r($files);
Array ( [0] => algorithm.php [1] => createDatabaseDoc.php [2] => createDatabaseDoc1.php [3] => index.php [4] => jsonToMarkDown.php [5] => dic.html )
带路径的
- $files = glob('../*.{php,html}', GLOB_BRACE);
- print_r($files);
Array ( [0] => ../test.php [1] => ../testwebservice.php [2] => ../aa.html [11] => ../bb.html [3] => ../element.html )
获取绝对路径
- $files = glob('../*.{php,html}', GLOB_BRACE);
- $files = array_map('realpath',$files);
- print_r($files);
Array ( [0] => D:\WWW\test.php [1] => D:\WWW\testwebservice.php [2] => D:\WWW\aa.html [3] => D:\WWW\element.html )
3.内存使用信息
memory_get_usage()能返回当前分配给PHP脚本的内存量,单位是字节(byte)
memory_get_peak_usage()函数返回内存使用峰值
- echo "Initial: ".memory_get_usage()." bytes <br>";
- // 使用内存
- for ($i = 0; $i < 100000; $i++) {
- $array []= md5($i);
- }
- echo "center: ".memory_get_usage()." bytes <br>";
- // 删除一半的内存
- for ($i = 0; $i < 100000; $i++) {
- unset($array[$i]);
- }
- echo "Final: ".memory_get_usage()." bytes <br>";
- echo "Peak: ".memory_get_peak_usage()." bytes <br>";
Initial: 375448 bytes
center: 10169816 bytes
Final: 4569816 bytes
Peak: 10169888 bytes
4.cpu使用信息
getrusage()返回CUP使用情况
- print_r(getrusage());
Array ( [ru_majflt] => 14843 [ru_maxrss] => 19532 [ru_utime.tv_usec] => 717604 [ru_utime.tv_sec] => 0 [ru_stime.tv_usec] => 31200 [ru_stime.tv_sec] => 0 )
ru_majflt 页失效
ru_maxrss 最大驻留集大小
ru_utime.tv_usec 用户态时间 (microseconds)
ru_utime.tv_sec 用户态时间(seconds)
ru_stime.tv_usec 系统内核时间 (microseconds)
ru_stime.tv_sec 系统内核时间(seconds)
- for($i=0;$i<10000000;$i++) {
- }
- $data = getrusage();
- echo "User time: ".($data['ru_utime.tv_sec'] +$data['ru_utime.tv_usec'] / 1000000);
- echo "<br>";
- echo "System time: ".($data['ru_stime.tv_sec'] +$data['ru_stime.tv_usec'] / 1000000);
User time: 0.156001
System time: 0.0312
5.系统常量当前的行号 __LINE__
- print_r( "Line ".__LINE__);
6.生成唯一的id
- echo uniqid()."<br>";
- echo uniqid('XYZ_')."<br>";
- echo uniqid('',true)."<br>";
- echo uniqid('XYZ_',true)."<br>";
5cda679f156c6
XYZ_5cda679f15aae
5cda679f15e967.34610847
XYZ_5cda679f1627e6.70270663
7.序列化、json转换
序列化
- $old = [
- 'abc',
- [1,'xx'],
- '111'
- ];
- $string = serialize($old);
- print_r($string);
- echo "<br>";
- $new = unserialize($string);
- print_r($new);
a:3:{i:0;s:3:"abc";i:1;a:2:{i:0;i:1;i:1;s:2:"xx";}i:2;s:3:"111";}
Array ( [0] => abc [1] => Array ( [0] => 1 [1] => xx ) [2] => 111 )
- $old = [
- 'abc',
- [1,'xx'],
- '111'
- ];
- $string = json_encode($old);
- print_r($string);
- echo "<br>";
- $new = json_decode($string);
- print_r($new);
["abc",[1,"xx"],"111"]
Array ( [0] => abc [1] => Array ( [0] => 1 [1] => xx ) [2] => 111 )
8.字符串压缩
压缩函数:gzcompress gzdeflate gzencode
解压函数:gzuncompress gzinflate gzdecode
gzcompress使用的是ZLIB格式;
gzdeflate使用的是纯粹的DEFLATE格式;
gzencode使用的是GZIP格式;
压缩数据时都使用了DEFLATE压缩算法
性能的维度看:DEFLATE好于 GZIP好于 ZLIB
从文本文件默认压缩率压缩后体积的维度看:DEFLATE好于 ZLIB好于 GZIP
(1)gzcompress和 gzuncompress
- $string =
- "Aspose.Slides for Java can be used with any kind of application either it's a Web or Desktop Application. Moreover, Aspose.Slides for Java also provides fully featured demos and working examples for developers to have a better understanding of the API.
- Aspose.Slides for Java can not only open PowerPoint files from different sources, but it also allows you to save your presentations in a variety of ways. For example, you can save your presentation in not only PPT. Moreover, you can also save your slides as images. Aspose.Slides for Java gives you almost all those features that you may or may not find in Microsoft PowerPoint. The versatility of Aspose.Slides for Java is that you can not only add tables and different kinds of shapes to your slides, but also different kinds of frames (that is text, audio and video frames).";
- // 压缩
- $compressed = gzcompress($string);
- echo "Original size: ". strlen($string)."<br>";
- echo "Compressed size: ". strlen($compressed)."<br>";
- // 解压缩
- $original = gzuncompress($compressed);
Original size: 831
Compressed size: 407
(2)gzdeflate 和 gzinflate
- $string =
- "Aspose.Slides for Java can be used with any kind of application either it's a Web or Desktop Application. Moreover, Aspose.Slides for Java also provides fully featured demos and working examples for developers to have a better understanding of the API.
- Aspose.Slides for Java can not only open PowerPoint files from different sources, but it also allows you to save your presentations in a variety of ways. For example, you can save your presentation in not only PPT. Moreover, you can also save your slides as images. Aspose.Slides for Java gives you almost all those features that you may or may not find in Microsoft PowerPoint. The versatility of Aspose.Slides for Java is that you can not only add tables and different kinds of shapes to your slides, but also different kinds of frames (that is text, audio and video frames).";
- // 压缩
- $compressed = gzdeflate($string);
- echo "Original size: ". strlen($string)."<br>";
- echo "Compressed size: ". strlen($compressed)."<br>";
- // 解压缩
- $original = gzinflate($compressed);
Original size: 831
Compressed size: 401
(3)gzencode 和 gzdecode
- $string =
- "Aspose.Slides for Java can be used with any kind of application either it's a Web or Desktop Application. Moreover, Aspose.Slides for Java also provides fully featured demos and working examples for developers to have a better understanding of the API.
- Aspose.Slides for Java can not only open PowerPoint files from different sources, but it also allows you to save your presentations in a variety of ways. For example, you can save your presentation in not only PPT. Moreover, you can also save your slides as images. Aspose.Slides for Java gives you almost all those features that you may or may not find in Microsoft PowerPoint. The versatility of Aspose.Slides for Java is that you can not only add tables and different kinds of shapes to your slides, but also different kinds of frames (that is text, audio and video frames).";
- // 压缩
- $compressed = gzencode($string);
- echo "Original size: ". strlen($string)."<br>";
- echo "Compressed size: ". strlen($compressed)."<br>";
- // 解压缩
- $original = gzdecode($compressed);
Original size: 831
Compressed size: 419
- $string =
- "As an example, with traditional programming, the main function of an application might make function calls into a menu library to display a list of available commands and query the user to select one.[4] The library thus would return the chosen option as the value of the function call, and the main function uses this value to execute the associated command. This style was common in text based interfaces. For example, an email client may show a screen with commands to load new mail, answer the current mail, start a new mail, etc., and the program execution would block until the user presses a key to select a command.
- With inversion of control, on the other hand, the program would be written using a software framework that knows common behavioral and graphical elements, such as windowing systems, menus, controlling the mouse, and so on. The custom code 'fills in the blanks' for the framework, such as supplying a table of menu items and registering a code subroutine for each item, but it is the framework that monitors the user's actions and invokes the subroutine when a menu item is selected. In the mail client example, the framework could follow both the keyboard and mouse inputs and call the command invoked by the user by either means, and at the same time monitor the network interface to find out if new messages arrive and refresh the screen when some network activity is detected. The same framework could be used as the skeleton for a spreadsheet program or a text editor. Conversely, the framework knows nothing about Web browsers, spreadsheets or text editors; implementing their functionality takes custom code.
- Inversion of control carries the strong connotation that the reusable code and the problem-specific code are developed independently even though they operate together in an application. Software frameworks, callbacks, schedulers, event loops, dependency injection, and the template method are examples of design patterns that follow the inversion of control principle, although the term is most commonly used in the context of object-oriented programming.";
- echo "Original size: ". strlen($string)."<br>";
- $gzcompress = gzdeflate($string);
- echo "gzcompress Compressed size: ". strlen($gzcompress)."<br>";$gzdeflate = gzdeflate($string);
- echo "gzdeflate Compressed size: ". strlen($gzdeflate)."<br>";$gzencode = gzencode($string);
- echo "gzencode Compressed size: ". strlen($gzencode)."<br>";
Original size: 2096
gzcompress Compressed size: 967
gzdeflate Compressed size: 967
gzencode Compressed size: 985
9.注册停止函数
register_shutdown_function()这个函数,能够在脚本终止前回调注册的函数,也就是当 PHP 程序执行完成后执行的函数。
- $clean = false;
- function shutdown_func()
- {
- global $clean;
- if (!$clean) {
- die("not a clean shutdown");
- }
- return false;
- }
- $a = 1;
- $a = new FooClass(); // 将因为致命错误而失败
- $clean = true;
Fatal error: Uncaught Error: Class 'FooClass' not found in D:\WWW\showdoc\algorithm.php:14 Stack trace: #0 {main} thrown in D:\WWW\showdoc\algorithm.php on line 14
- <?php
- $clean = false;
- function shutdown_func()
- {
- global $clean;
- if (!$clean) {
- die("not a clean shutdown");
- }
- return false;
- }
- register_shutdown_function("shutdown_func");
- $a = 1;
- $a = new FooClass(); // 将因为致命错误而失败
- $clean = true;
Fatal error: Uncaught Error: Class 'FooClass' not found in D:\WWW\showdoc\algorithm.php:14 Stack trace: #0 {main} thrown in D:\WWW\showdoc\algorithm.php on line 14
not a clean shutdown
register_shutdown_function执行机制是:
PHP把要调用的函数调入内存。当页面所有PHP语句都执行完成时,再调用此 函数。
在这个时候从内存中调用,不是从PHP页面中调用,所以上面的例子不能使用相对路径,因为PHP已经当原来的页面不存在了
调用条件:
1、当页面被用户强制停止时
2、当程序代码运行超时时
3、当PHP代码执行完成时,代码执行存在异常和错误、警告
php实用功能小记的更多相关文章
- [原]Paste.deploy 与 WSGI, keystone 小记
Paste.deploy 与 WSGI, keystone 小记 名词解释: Paste.deploy 是一个WSGI工具包,用于更方便的管理WSGI应用, 可以通过配置文件,将WSGI应用加载起来. ...
- MySql 小记
MySql 简单 小记 以备查看 1.sql概述 1.什么是sql? 2.sql发展过程? 3.sql标准与方言的关系? 4.常用数据库? 5.MySql数据库安装? 2.关键概念 表结构----- ...
- Git小记
Git简~介 Git是一个分布式版本控制系统,其他的版本控制系统我只用过SVN,但用的时间不长.大家都知道,分布式的好处多多,而且分布式已经包含了集中式的几乎所有功能.Linus创造Git的传奇经历就 ...
- 广州PostgreSQL用户会技术交流会小记 2015-9-19
广州PostgreSQL用户会技术交流会小记 2015-9-19 今天去了广州PostgreSQL用户会组织的技术交流会 分别有两个session 第一个讲师介绍了他公司使用PostgreSQL-X2 ...
- 东哥读书小记 之 《MacTalk人生元编程》
一直以来的自我感觉:自己是个记性偏弱的人.反正从小读书就喜欢做笔记(可自己的字写得巨丑无比,尼玛不科学呀),抄书这事儿真的就常发生俺的身上. 因为那时经常要背诵课文之类,反正为了怕自己忘记, ...
- Paypal支付小记
Paypal支付小记 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impo ...
- linux 下cmake 编译 ,调用,调试 poco 1.6.0 小记
上篇文章 小记了: 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记. http://www.cnblogs.com/bleachli/p/4352 ...
- mongodb入门学习小记
Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...
- 【日常小记】统计后缀名为.cc、.c、.h的文件数【转】
转自:http://www.cnblogs.com/skynet/archive/2011/03/29/1998970.html 在项目开发时,有时候想知道源码文件中有多少后缀名为.cc..c..h的 ...
随机推荐
- CentOS6.5配置
关闭防火墙 查看防火墙状态 /etc/init.d/iptables status 停止 /etc/init.d/iptables stop 开机不启动 chkconfig iptables off ...
- Linux shell脚本基础学习详细介绍(完整版)一
Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Lin ...
- 如何使用 淘宝 NPM 镜像
淘宝 NPM 镜像 原文链接 http://npm.taobao.org/ 这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同 ...
- [CSS] prefers-reduced-motion
The prefers-reduced-motion CSS media feature is used to detect if the user has requested that the sy ...
- PostgreSQL 一些比较好用的字符串函数
最近刚接触到PostgreSQL数据库,发现很多功能比较强大的内置函数,特此记录下来.示例下次再补. 1.concat 字符串连接函数 2.concat_ws concat_ws函数连接可自定义分隔符 ...
- BZOJ 3451: Tyvj1953 Normal 点分治+FFT
根据期望的线性性,我们算出每个点期望被计算次数,然后进行累加. 考虑点 $x$ 对点 $y$ 产生了贡献,那么说明 $(x,y)$ 之间的点中 $x$ 是第一个被删除的. 这个期望就是 $\frac{ ...
- 如何用Windbg从dump获取计算机名、主机名
对内存转储时发生的事情有一定的了解是非常重要的.这有助于您确定要执行哪些WinDbg命令,并为您提供一些有关如何解释这些命令输出的上下文.我正在查看一个服务器的内存转储,该服务器存在性能问题.我在内存 ...
- tldr/cheat
tldr 比man好用的查询命令查询工具, man很强大,但是 TLDR,too long dont read 安装 npm install -g tldr 使用说明 其他版本下载 https://g ...
- 在触发器中使用{ITEM.LASTVALUE}时在首页问题栏信息显示不全
在触发器中使用了系统宏变量,当条件满足时,如果这个宏代表的内容超过了20个字符,那么在首页信息就显示不全,会有一堆省略号 感谢https://blog.csdn.net/yu415907917/art ...
- 2017.10.1 国庆清北 D1T1 zhx的字符串题
题目背景 2017国庆清北D1T1 题目描述 你是能看到第一题的 friends 呢. ——hja 何大爷对字符串十分有研究,于是天天出字符串题虐杀 zhx.何大爷今天为 字符串定义了新的权值计算方法 ...