Home Backend Development PHP Tutorial 九个你需要知道的PHP函数和功能

九个你需要知道的PHP函数和功能

Jun 13, 2016 pm 01:02 PM
array echo nbsp

9个你需要知道的PHP函数和功能
即使使用 PHP 多年,有些功能和特点我们也未必发现或未被充分利用,一旦被我们发现,就会发现它们非常有用。然而,并不是所有的人都已经从头至尾详读过 PHP 的手册和功能参考!

1. 函数与任意数量的参数
您可能已经知道,PHP 允许我们定义可选参数的函数。但也有完全允许任意数量的函数参数方法。

首先,下面这个例子只是可选参数:

view sourceprint?01 // function with 2 optional arguments 

02 function foo($arg1 = '', $arg2 = '') { 

03     echo "arg1: $arg1\n"; 

04     echo "arg2: $arg2\n"; 

05 } 

06 foo('hello','world'); 

07 /* prints: 

08 arg1: hello 

09 arg2: world 

10 */

11 foo(); 

12 /* prints: 

13 arg1: 

14 arg2: 

15 */

现在,让我们看看如何可以建立一个函数接受任何数量的参数。这一次,我们要利用 func_get_args() 函数:

view sourceprint?01 // yes, the argument list can be empty 

02 function foo() { 

03     // returns an array of all passed arguments 

04     $args = func_get_args(); 

05     foreach ($args as $k => $v) { 

06         echo "arg".($k+1).": $v\n"; 

07     } 

08 } 

09 foo(); 

10 /* prints nothing */

11 foo('hello'); 

12 /* prints 

13 arg1: hello 

14 */

15 foo('hello', 'world', 'again'); 

16 /* prints 

17 arg1: hello 

18 arg2: world 

19 arg3: again 

20 */

2. 使用 Glob() 函数来查找文件
许多 PHP 内置函数有非常长的命名。然而,它可能会很难说明是什么作用的函数,如果不使用 Glob() 来做,除非你已经非常熟悉这个函数。

它更像是 scandir() 函数加强型版本。它可以让您通过使用模式搜索文件。

view sourceprint?01 // get all php files 

02 $files = glob('*.php'); 

03 print_r($files); 

04 /* output looks like: 

05 Array 

06 ( 

07     [0] => phptest.php 

08     [1] => pi.php 

09     [2] => post_output.php 

10     [3] => test.php 

11 ) 

12 */

这样你可以获得多个文件类型:

view sourceprint?01 // get all php files AND txt files 

02 $files = glob('*.{php,txt}', GLOB_BRACE); 

03 print_r($files); 

04 /* output looks like: 

05 Array 

06 ( 

07     [0] => phptest.php 

08     [1] => pi.php 

09     [2] => post_output.php 

10     [3] => test.php 

11     [4] => log.txt 

12     [5] => test.txt 

13 ) 

14 */

请注意,这些文件其实是可以返回一个路径的,根据你的查询。

view sourceprint?1 $files = glob('../images/a*.jpg'); 

2 print_r($files); 

3 /* output looks like: 

4 Array 

5 ( 

6     [0] => ../images/apple.jpg 

7     [1] => ../images/art.jpg 

8 ) 

9 */

如果你想获得每个文件的完整路径,你可以调用 realpath() 函数来返回。

view sourceprint?01 $files = glob('../images/a*.jpg'); 

02 // applies the function to each array element 

03 $files = array_map('realpath',$files); 

04 print_r($files); 

05 /* output looks like: 

06 Array 

07 ( 

08     [0] => C:\wamp\www\images\apple.jpg 

09     [1] => C:\wamp\www\images\art.jpg 

10 ) 

11 */

3. 内存使用信息
通过观察你的脚本内存使用情况,你就可以将你的代码进行针对性优化。

PHP 有一个垃圾收集器和一个相当复杂的内存管理器。当你的脚本开始就开始正式使用内存。也会根据脚本的执行情况,内存使用量会上升也会下降。为了得到当前内存使用情况,我们就可以使用 memory_get_usage() 函数。并可以在任何时候得到内存使用的最高点,下面就是使用 memory_get_usage() 函数的例子。

view sourceprint?01 echo "Initial: ".memory_get_usage()." bytes \n"; 

02 /* prints 

03 Initial: 361400 bytes 

04 */

05 // let's use up some memory 

06 for ($i = 0; $i
07     $array []= md5($i); 

08 } 

09 // let's remove half of the array 

10 for ($i = 0; $i
11     unset($array[$i]); 

12 } 

13 echo "Final: ".memory_get_usage()." bytes \n"; 

14 /* prints 

15 Final: 885912 bytes 

16 */

17 echo "Peak: ".memory_get_peak_usage()." bytes \n"; 

18 /* prints 

19 Peak: 13687072 bytes 

20 */

4. CPU 的使用信息
为此,我们就要利用 getrusage() 函数。请记住,这个函数不能应用于 windows 平台。

view sourceprint?01 print_r(getrusage()); 

02 /* prints 

03 Array 

04 ( 

05     [ru_oublock] => 0 

06     [ru_inblock] => 0 

07     [ru_msgsnd] => 2 

08     [ru_msgrcv] => 3 

09     [ru_maxrss] => 12692 

10     [ru_ixrss] => 764 

11     [ru_idrss] => 3864 

12     [ru_minflt] => 94 

13     [ru_majflt] => 0 

14     [ru_nsignals] => 1 

15     [ru_nvcsw] => 67 

16     [ru_nivcsw] => 4 

17     [ru_nswap] => 0 

18     [ru_utime.tv_usec] => 0 

19     [ru_utime.tv_sec] => 0 

20     [ru_stime.tv_usec] => 6269 

21     [ru_stime.tv_sec] => 0 

22 ) 

23 */

这看起来蛮神秘的,有些艰涩难懂,除非你已经有过系统管理员的经验,以下是每个值的介绍(或许你并不需要记住这些):

ru_oublock:块输出操作
ru_inblock:块输入操作
ru_msgsnd:邮件发送
ru_msgrcv:收到的邮件
ru_maxrss:最大驻留集大小
ru_ixrss:积分共享内存的大小
ru_idrss:积分大小非共享数据
ru_minflt:页回收
ru_majflt:页面错误
ru_nsignals:信号接收
ru_nvcsw:自动上下文切换
ru_nivcsw:非自动的上下文切换
ru_nswap:过期
ru_utime.tv_usec:用户使用时间(微秒)
ru_utime.tv_sec:用户使用时间(秒)
ru_stime.tv_usec:系统使用时间(微秒)
ru_stime.tv_sec:系统使用时间(秒)
要看 CPU 的功率有多少被脚本消耗,我们需要观察 user time 和 system time 的值。秒和毫秒是默认独立提供的。你可以将 100 万毫秒的值,并将其换算成秒的值,将它当做一个十进制数的总秒数。

让我们看一个例子:

view sourceprint?01 // sleep for 3 seconds (non-busy) 

02 sleep(3); 

03 $data = getrusage(); 

04 echo "User time: ". 

05     ($data['ru_utime.tv_sec'] + 

06     $data['ru_utime.tv_usec'] / 1000000); 

07 echo "System time: ". 

08     ($data['ru_stime.tv_sec'] + 

09     $data['ru_stime.tv_usec'] / 1000000); 

10 /* prints 

11 User time: 0.011552 

12 System time: 0 

13 */

虽然脚本大约花了 3 秒钟的时间来运行, CPU 的使用率还是非常非常低的。因为 sleep 工作,脚本实际上并没有消耗 CPU 资源。当然还有其他的任务可能真正需要等待时间,但千万不能用磁盘的读取写入操作来等待 CPU 时间。所以你可以发现, CPU 使用率和运行时的实际长度并不是总是一样的。

下面是另外一个例子。

view sourceprint?01 // loop 10 million times (busy) 

02 for($i=0;$i
03 } 

04 $data = getrusage(); 

05 echo "User time: ". 

06     ($data['ru_utime.tv_sec'] + 

07     $data['ru_utime.tv_usec'] / 1000000); 

08 echo "System time: ". 

09     ($data['ru_stime.tv_sec'] + 

10     $data['ru_stime.tv_usec'] / 1000000); 

11 /* prints 

12 User time: 1.424592 

13 System time: 0.004204 

14 */

这花了大约 1.4 秒的 CPU 时间。几乎所有这些都是由用户操作所用的时间,系统并没有被调用。

系统时间是划分时间的 CPU 上执行的程序的代表的内核系统调用时间。(谁有更简明扼要的描述?help!)下面是一个例子:

view sourceprint?01 $start = microtime(true); 

02 // keep calling microtime for about 3 seconds 

03 while(microtime(true) - $start
04 } 

05 $data = getrusage(); 

06 echo "User time: ". 

07     ($data['ru_utime.tv_sec'] + 

08     $data['ru_utime.tv_usec'] / 1000000); 

09 echo "System time: ". 

10     ($data['ru_stime.tv_sec'] + 

11     $data['ru_stime.tv_usec'] / 1000000); 

12 /* prints 

13 User time: 1.088171 

14 System time: 1.675315 

15 */

5. 魔术常量
PHP 提供了获取当前行号的方法 (__LINE__),获取文件路径方法(__FILE__),目录(__DIR__),函数名(__FUNCTTION__),类名(__CLASS__),方法名(__METHOD__),和命名空间(__NAMESPACE__)。以上就是常用的魔术常量。恐怕我们最常用的就只有 (__FILE__) 了。

Rikku 不打算全部进行说明,但会说几个用例。

当然包括了其他的脚本,这是个不错的主意。((__DIR__)需要 PHP 5.3 以上版本):

view sourceprint?1 // this is relative to the loaded script's path 

2 // it may cause problems when running scripts from different directories 

3 require_once('config/database.php'); 

4 // this is always relative to this file's path 

5 // no matter where it was included from 

6 require_once(dirname(__FILE__) . '/config/database.php');

使用 __LINE__ 让调试更加容易,你可以跟踪行号:

view sourceprint?01 // some code 

02 // ... 

03 my_debug("some debug message", __LINE__); 

04 /* prints 

05 Line 4: some debug message 

06 */

07 // some more code 

08 // ... 

09 my_debug("another debug message", __LINE__); 

10 /* prints 

11 Line 11: another debug message 

12 */

13 function my_debug($msg, $line) { 

14     echo "Line $line: $msg\n"; 

15 }

6. 生成唯一的ID
有些情况下,您需要生成一股唯一的字符串。我看到很多人会用这个 md5() 函数,即使他并不完全用于此目的的存在:

view sourceprint?1 // generate unique string 

2 echo md5(time() . mt_rand(1,1000000));

其实有个专门的 PHP 函数,名为 uniqid() 就是为了这个目的而存在的:

view sourceprint?01 // generate unique string 

02 echo uniqid(); 

03 /* prints 

04 4bd67c947233e 

05 */

06 // generate another unique string 

07 echo uniqid(); 

08 /* prints 

09 4bd67c9472340 

10 */

您可能会注意到,即使是唯一的字符串,他们的前几个字符很相似。这是因为生成的字符串是关联到服务器时间的。设实际上有一个非常好的副作用,因为每个新生成的 ID 将在生成后按字母顺序排列,这样也省去了我们排序的逻辑动作。

为了减少重复的几率,你可以传递一个前缀,或在第二个参数来增加。

view sourceprint?01 // with prefix 

02 echo uniqid('foo_'); 

03 /* prints 

04 foo_4bd67d6cd8b8f 

05 */

06 // with more entropy 

07 echo uniqid('',true); 

08 /* prints 

09 4bd67d6cd8b926.12135106 

10 */

11 // both 

12 echo uniqid('bar_',true); 

13 /* prints 

14 bar_4bd67da367b650.43684647 

15 */

此功能将会生成比 md5() 生成的字符串更短,这也将节省您的空间。

7. 序列化
你有没有需要存储在数据库中复杂的变量或者大文本文件?那你有没有拿出一个解决方法,花式转换成格式化的字符串数组或对象的?别担心,PHP 已经为我们准备好了这个功能。

有两种序列化的方法,下面是一个例子,它使用 serialize() 进行序列化和 unserialize() 进行解除序列化:

view sourceprint?01 // a complex array 

02 $myvar = array( 

03     'hello', 

04     42, 

05     array(1,'two'), 

06     'apple'

07 ); 

08 // convert to a string 

09 $string = serialize($myvar); 

10 echo $string; 

11 /* prints 

12 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";} 

13 */

14 // you can reproduce the original variable 

15 $newvar = unserialize($string); 

16 print_r($newvar); 

17 /* prints 

18 Array 

19 ( 

20     [0] => hello 

21     [1] => 42 

22     [2] => Array 

23         ( 

24             [0] => 1 

25             [1] => two 

26         ) 

27     [3] => apple 

28 ) 

29 */

这是原生态的 PHP 序列化方法。然而,由于 JSON 近年来已经大受欢迎,PHP 5.2 中也决定添加对它们的支持。现在你可以使用 json_encode() 和 json_decode() 函数来完成这项工作:

view sourceprint?01 // a complex array 

02 $myvar = array( 

03     'hello', 

04     42, 

05     array(1,'two'), 

06     'apple'

07 ); 

08 // convert to a string 

09 $string = json_encode($myvar); 

10 echo $string; 

11 /* prints 

12 ["hello",42,[1,"two"],"apple"] 

13 */

14 // you can reproduce the original variable 

15 $newvar = json_decode($string); 

16 print_r($newvar); 

17 /* prints 

18 Array 

19 ( 

20     [0] => hello 

21     [1] => 42 

22     [2] => Array 

23         ( 

24             [0] => 1 

25             [1] => two 

26         ) 

27     [3] => apple 

28 ) 

29 */

这么做看起来会更加紧凑。当然它对其他语言如 javascript 兼容性也是最好的。然而,您需要注意的是:对于某些复杂的对象,某些信息会无故丢失!

8. 压缩字符串
在谈到压缩时,我们通常会想到一些文件,如 zip 文件。它可以在 PHP 中压缩长字符串,并且不涉及任何存档文件。

在下面的例子中,我们要利用 gzcompress() 和 gzuncompress() 函数:

view sourceprint?01 $string = 

02 "Lorem ipsum dolor sit amet, consectetur 

03 adipiscing elit. Nunc ut elit id mi ultricies 

04 adipiscing. Nulla facilisi. Praesent pulvinar, 

05 sapien vel feugiat vestibulum, nulla dui pretium orci, 

06 non ultricies elit lacus quis ante. Lorem ipsum dolor 

07 sit amet, consectetur adipiscing elit. Aliquam 

08 pretium ullamcorper urna quis iaculis. Etiam ac massa 

09 sed turpis tempor luctus. Curabitur sed nibh eu elit 

10 mollis congue. Praesent ipsum diam, consectetur vitae 

11 ornare a, aliquam a nunc. In id magna pellentesque 

12 tellus posuere adipiscing. Sed non mi metus, at lacinia 

13 augue. Sed magna nisi, ornare in mollis in, mollis 

14 sed nunc. Etiam at justo in leo congue mollis. 

15 Nullam in neque eget metus hendrerit scelerisque 

16 eu non enim. Ut malesuada lacus eu nulla bibendum 

17 id euismod urna sodales. "; 

18 $compressed = gzcompress($string); 

19 echo "Original size: ". strlen($string)."\n"; 

20 /* prints 

21 Original size: 800 

22 */

23 echo "Compressed size: ". strlen($compressed)."\n"; 

24 /* prints 

25 Compressed size: 418 

26 */

27 // getting it back 

28 $original = gzuncompress($compressed);

我们能够压缩近 50% 。另外 gzencode() 和 gzdecode() 可以达成类似的结果,但通过的是不同的压缩算法。

9. register_shutdown_function
有一个函数叫 register_shutdown_function(),可以让你在拥有执行一些代码权限之前,完成脚本的运行。

试想一下,你想捕捉到你脚本执行至结束时一些基准的统计数据,如一共用了多少时间来执行:

view sourceprint?1 // capture the start time 

2 $start_time = microtime(true); 

3 // do some stuff 

4 // ... 

5 // display how long the script took 

6 echo "execution took: ". 

7         (microtime(true) - $start_time). 

8         " seconds.";

起初觉得这些似乎是微不足道的。你只要添加代码放在底部,它运行脚本之前完成。不过,在脚本程序其中你调用了 exit() 函数,那么该段代码将不被执行。此外,如果有一个致命的错误,或者该脚本由用户终止(就是按浏览器上面的停止按钮),再次刷新页面也无法被运行。

当您使用 register_shutdown_function(),你的代码将没有理由被迫停止:

view sourceprint?01 $start_time = microtime(true); 

02 register_shutdown_function('my_shutdown'); 

03 // do some stuff 

04 // ... 

05 function my_shutdown() { 

06     global $start_time; 

07     echo "execution took: ". 

08             (microtime(true) - $start_time). 

09             " seconds."; 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles