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 functions commonly used by PHP programmers

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

Share

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

This article mainly introduces "what are the functions commonly used by PHP programmers". In daily operation, I believe many people have doubts about which functions are commonly used by PHP programmers. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "which functions are commonly used by PHP programmers." Next, please follow the editor to study!

1. Pass any number of function parameters

In .NET or JAVA programming, we usually have a fixed number of function arguments, but PHP allows you to use any number of arguments. 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:

The copy code is as follows:

/ / 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 the file

The function names of most PHP functions 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, 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 an 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. Get memory usage information

PHP's memory recovery mechanism is already 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, and 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. Here are some explanations:

Ru_oublock: block output operation

Ru_inblock: block input operation

Ru_msgsnd: message sent

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 collection

Ru_majflt: page invalidation

Ru_nsignals: received signal

Ru_nvcsw: active context switching

Ru_nivcsw: passive context switching

Ru_nswap: exchange area

Ru_utime.tv_usec: time in user mode (microseconds)

Ru_utime.tv_sec: time in user mode (seconds)

Ru_stime.tv_usec: system kernel time (microseconds)

Ru_stime.tv_sec: system kernel time? (seconds)

To 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

, /

Sleep does not 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