Welcome to the Linux community forum and interact with 2 million technicians. Below are nine useful PHP functions. Do you know you have used them? 1. Any number of function parameters 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 all arbitrary parameters. The following is an example.
Welcome to the Linux community forum and interact with 2 million technical staff> below are nine useful PHP functions. Do you know you have used them? 1. Any number of function parameters 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 all arbitrary parameters. The following is an example.
Welcome to the Linux community forum and interact with 2 million technicians>
Below are nine useful functions in PHP. Do you know you have used them?
1. Any number of parameters of the Function
You may know that PHP allows you to define a default parameter function. But you may not know that PHP also allows you to define a function with all arbitrary parameters.
The following example shows the default parameter functions:
// Functions with two default parameters
Function foo ($ arg1 = '', $ arg2 = ''){
Ho "arg1: $ arg1n ";
Ho "arg2: $ arg2n ";
}
Foo ('hello', 'World ');
/* Output:
Arg1: hello
Arg2: world
*/
Foo ();
/* Output:
Arg1:
Arg2:
*/
2. Use Glob () to find files
Many PHP functions have a long self-explanatory function name? During glob (), you may not know what this function is for, unless you are familiar with it.
Do you think this function is good? Like scandir (), scandir can be used to find files.
// Retrieve the PHP and TXT files
$ 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
)
*/
3. memory usage information
Observe that the memory usage of your program allows you to better optimize your code.
PHP has a garbage collection mechanism and a complicated memory management mechanism. You can know the memory usage of your script. Do you want to know the current memory usage? Memory_get_usage () function. If you want to know the peak memory usage, you can call the memory_get_peak_usage () function.
Echo "Initial:". memory_get_usage (). "Bytes n ";
/* Output
Initialize: 361400 bytes
*/
// Memory usage
For ($ I = 0; I I <100000; $ I ++ ){
$ Array [] = md5 ($ I );
}
// Delete half of the memory
For ($ I = 0; I 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 ";
/* Output peak value
Peak: 13687072 bytes
*/
4. CPU usage information
Use? The getrusage () function allows you to know the CPU usage. Note that this function is not available in Windows.
View code printing Code help
Ru_oublock: block output operation
Ru_inblock: block input operation ru_msgsnd: sent message
Ru_msgrcv: Received message
Ru_maxrss: the largest and smallest resident set
Ru_ixrss: All shared memory size
Ru_idrss: all non-shared memory sizes
Ru_minflt: Page recycling
Ru_majflt: the page is invalid.
Ru_nsignals: Received Signal
Ru_nvcsw: Active context switching
Ru_nivcsw: passive context switching
Ru_nswap: swap Zone
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)
5. System Constants
PHP provides very useful system constants for you to get the current row number (_ LINE _), FILE (_ FILE _), directory (_ DIR __), FUNCTION Name (_ FUNCTION _), CLASS Name (_ CLASS _), METHOD Name (_ METHOD _), and NAMESPACE (_ NAMESPACE __), similar to C language.
We can think that these things are mainly used for debugging, but they are not necessarily the same. For example, we can use them when include other files? _ FILE _ (of course, you can also use _ DIR _ after PHP 5.3 __)
6. Generate a unique ID
Many users use md5 () to generate a unique ID, as shown below:
In fact, there is one in PHP called? The uniqid () function is specifically used to do this:
// Generate unique string
Echo uniqid ();
/* Output
4bd67c947233e
*/
// Generate another unique string
Echo uniqid ();
/* Output
4bd67c9472340
*/
7. serialization
Will you store a complicated data structure in a database or file? You do not need to write your own algorithms. PHP has already been ready for you. It provides two functions :? Serialize () and unserialize ():
8. String Compression
When we talk about compression, we may think of File compression. In fact, strings can also be compressed. PHP provides? Gzcompress () and gzuncompress () functions:
$ 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 presponorci,
Non ultricies elit lacus quis ante. Lorem ipsum dolor
Sit amet, consectetur adipiscing elit. Aliquam
Prepolicullamcorper 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 ";
/* Original output size
Original size: 800
*/
Echo "Compressed size:". strlen ($ compressed ). "N ";
/* Output compressed size
Compressed size: 418
*/
// Extract
$ Original = gzuncompress ($ compressed );
The compression ratio is almost 50%. At the same time, you can also use? Gzencode () and gzdecode () functions to compress, instead of using different compression algorithms.
9. register the stop Function
Is there a function called? Register_shutdown_function () allows you to run the code before the entire script is stopped. Let's take a look at the following example:
// 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 preceding example is only used to calculate the running time of a function. Then, if you call it in the middle of the function? Exit () function, then your final code will not be run. In addition, if the script is terminated in the browser (the user presses the stop button), it cannot be run. When register_shutdown_function () is used, your program runs even after the script is stopped: