Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the necessary PHP function codes?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what are the necessary PHP function codes". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what are the necessary PHP function codes"!

Programmers who have done PHP development should know that there are many built-in functions in PHP, and mastering them can help you to do PHP development more handy. This article will share 8 essential PHP functions, all of which are very practical. I hope PHP developers can master them.

1. Pass any number of function parameters

2. Use glob () to find the file

3. Get memory usage information

4. Obtain CPU usage information

5. Get the system constant

6. Generate a unique id

7. Serialization

8. String compression

1, pass any number of function parameters We in .NET or JAVA programming, the general number of function parameters are fixed, but PHP allows you to use any number of parameters. The following example shows you the default parameters of the PHP function:

The copy code is as follows:

/ / functions with two default parameters

Function foo ($arg1 = ", $arg2 =") {

Echo "arg1: $arg1\ n"

Echo "arg2: $arg2\ n"

}

Foo ('hello','world')

/ * output:

Arg1: hello

Arg2: world

, /

Foo ()

/ * output:

Arg1:

Arg2:

, /

The following example is the indefinite parameter usage of PHP, which uses the func_get_args () method:

/ / Yes, the parameter list is empty

Function foo () {

/ / get an array of all the passed parameters

$args = func_get_args ()

Foreach ($args as $k = > $v) {

Echo "arg". ($kumb1). ": $v\ n"

}

}

Foo ()

/ * nothing will be output * /

Foo ('hello')

/ * output

Arg1: hello

, /

Foo ('hello',' world', 'again')

/ * output

Arg1: hello

Arg2: world

Arg3: again

, /

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 what it is used for. In fact, glob (), like scandir (), can be used to find files. Please see the following usage:

The copy code is as follows:

/ / get all files with the suffix PHP

$files = glob ('* .php')

Print_r ($files)

/ * output:

Array

(

[0] = > phptest.php

[1] = > pi.php

[2] = > post_output.php

[3] = > test.php

)

, /

You can also find a variety of suffix names:

The copy code is as follows:

/ / fetch PHP file and TXT file

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

Print_r ($files)

/ * output:

Array

(

[0] = > phptest.php

[1] = > pi.php

[2] = > post_output.php

[3] = > test.php

[4] = > log.txt

[5] = > test.txt

)

, /

You can also add a path:

The copy code is as follows:

$files = glob ('.. / images/a*.jpg')

Print_r ($files)

/ * output:

Array

(

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

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

)

, /

If you want the absolute path, you can call the realpath () function:

The copy code is as follows:

$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:\ wamp\ www\ images\ apple.jpg

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

)

, /

3, access to memory usage information PHP memory recovery mechanism has been very powerful, you can also use the PHP script to get the current memory usage, call the memory_get_usage () function to get the current memory usage, call the memory_get_peak_usage () function to get the peak memory usage. The reference code is as follows:

The copy code is as follows:

Echo "Initial:" .memory _ get_usage (). "bytes\ n"

/ * output

Initial: 361400 bytes

, /

/ / use memory

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下不可用。 复制代码 代码如下: 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

)

, /

This structure looks obscure unless you know a lot about CPU. The following are some explanations: ru_oublock: block output operation ru_inblock: block input operation ru_msgsnd: sent message ru_msgrcv: received message ru_maxrss: maximum resident set size ru_ixrss: all shared memory size ru_idrss: all non-shared memory size ru_minflt: page recycling ru_majflt: page failure ru_nsignals: received signal ru_nvcsw: active context cut Change ru_nivcsw: passive context switching ru_nswap: switch ru_utime.tv_usec: user state time (microseconds) ru_utime.tv_sec: user state time (seconds) ru_stime.tv_usec: system kernel time (microseconds) ru_stime.tv_sec: system kernel time? (seconds) see how much CPU your script consumes We need to look at the values of "user mode time" and "system kernel time". The second and microsecond parts are provided respectively, and you can divide the microsecond value by 1 million and add it to the second value to get the number of seconds with a fractional fraction.

The copy code is as follows:

/ / sleep for 3 seconds (non-busy)

Sleep (3)

$data = getrusage ()

Echo "User time:".

($data ['ru_utime.tv_sec'] +

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

Echo "System time:".

($data ['ru_stime.tv_sec'] +

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

/ * output

User time: 0.011552

System time: 0

, /

It doesn't take up system time, so let's take a look at the following example:

The copy code is as follows:

/ / loop 10 million times (busy)

For ($iSuppli

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report