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 common functions of php function

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

Share

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

Editor to share with you what the common functions of the php function are, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Any number of arguments to a function

You may know that PHP allows you to define a function with default parameters. But you may not know that PHP also allows you to define a function with completely arbitrary parameters.

Here is an example of a function that shows you the default parameters:

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:

, /

Now let's take a look at a function with indefinite parameters that 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". ($Kraft 1). ": $vPop"

}

}

Foo ()

/ * nothing will be output * /

Foo ('hello')

/ * output

Arg1: hello

, /

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

/ * output

Arg1: hello

Arg2: world

Arg3: again

, /

two。 Use Glob () to find the file

Many PHP functions have a long self-explanatory function name, but when you see glob (), you may not know what this function is for unless you are already familiar with it.

You can think of this function as good as scandir (), which can be used to find files.

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.

/ / 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. Memory usage information

Observing the memory usage of your program allows you to better optimize your code.

PHP has a garbage collection mechanism and a complex memory management mechanism. You can know how much memory your script uses. To know the current memory usage, you can use the memory_get_usage () function, and if you want to know the peak memory usage, you can call the memory_get_peak_usage () function.

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使用信息 使用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 is 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 / investors / hello)

[1] = > 42

[2] = > Array

(

[0] = > 1

[1] = > two

)

[3] = > apple

)

, /

This is the native function of PHP, but today JSON is becoming more and more popular, so after PHP5.2, PHP begins to support JSON, and you can use the json_encode () and json_decode () functions

The copy code is as follows:

/ / a complex array

$myvar = array (

'hello'

forty-two,

Array (1 recorder two')

'apple'

);

/ / convert to a string

$string = json_encode ($myvar)

Echo $string

/ * prints

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

, /

/ / you can reproduce the original variable

$newvar = json_decode ($string)

Print_r ($newvar)

/ * prints

Array

(

[0] = > hello

[1] = > 42

[2] = > Array

(

[0] = > 1

[1] = > two

)

[3] = > apple

)

, /

This looks a little more compact and is also compatible with Javascript and other languages. But for some very complex data structures, it may result in data loss.

8. String compression

When we talk about compression, we may think of file compression. In fact, strings can also be compressed. PHP provides the gzcompress () and gzuncompress () functions:

The copy code is as follows:

$string =

"Lorem ipsum dolor sit amet, consectetur

Adipiscing elit. Nunc ut elit id mi ultricies

Adipiscing. Nulla facilisi. Praesent pulvinar

Sapien vel feugiat vestibulum, nulla dui pretium orci

Non ultricies elit lacus quis ante. Lorem ipsum dolor

Sit amet, consectetur adipiscing elit. Aliquam

Pretium ullamcorper urna quis iaculis. Etiam ac massa

Sed turpis tempor luctus. Curabitur sed nibh eu elit

Mollis congue. Praesent ipsum diam, consectetur vitae

Ornare a, aliquam a nunc. In id magna pellentesque

Tellus posuere adipiscing. Sed non mi metus, at lacinia

Augue. Sed magna nisi, ornare in mollis in, mollis

Sed nunc. Etiam at justo in leo congue mollis.

Nullam in neque eget metus hendrerit scelerisque

Eu non enim. Ut malesuada lacus eu nulla bibendum

Id euismod urna sodales. "

$compressed = gzcompress ($string)

Echo "Original size:". Strlen ($string). "/ n"

/ * output original size

Original size: 800

, /

Echo "Compressed size:". Strlen ($compressed). "/ n"

/ * compressed output size

Compressed size: 418

, /

/ / decompress

$original = gzuncompress ($compressed)

There is almost a 50% compression ratio. At the same time, you can also use the gzencode () and gzdecode () functions to compress, without using different compression algorithms.

9. Register stop function

There is a function called register_shutdown_function () that allows you to run code before the entire script stops. Let's look at the following example:

The copy code is as follows:

/ / capture the start time

$start_time = microtime (true)

/ / do some stuff

/ /...

/ / display how long the script took

Echo "execution took:".

(microtime (true)-$start_time)

"seconds."

The above example is only used to calculate the time for a function to run. Then, if you call the exit () function in the middle of the function, your final code will not be run to. Also, if the script terminates in the browser (the user presses the stop button), it cannot be run.

When we use register_shutdown_function (), your program will be run even after the script is stopped:

The copy code is as follows:

$start_time = microtime (true)

Register_shutdown_function ('my_shutdown')

/ / do some stuff

/ /...

Function my_shutdown () {

Global $start_time

Echo "execution took:".

(microtime (true)-$start_time)

"seconds."

}

These are all the contents of this article entitled "what are the common functions of php functions?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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