Developers who have done PHP development should be aware that PHP has a lot of built-in features, mastered them, can help you in the development of PHP more handy, this article will share 8 development of the necessary PHP features, all very practical, I hope that you PHP developers can master.
1, pass any number of function parameters we are in. NET or Java programming, the number of general function parameters is fixed, but PHP allows you to use any number of parameters. The following example shows you the default parameters for PHP functions: PHP code
- //two default parameter functions
- function foo ($arg 1 = ", $arg 2 =") {
- echo "Arg1: $arg 1 \ n ";
- echo "arg2: $arg 2\n";
- }
- foo (' Hello ', ' world ');
- /* Output:
- Arg1:hello
- Arg2:world
- */
- foo ();
- /* Output:
- arg1:
- arg2:
- */
- The following example is an indeterminate parameter usage for PHP that uses [Url=http://us2.php.net/manual/en/function.func-get-args.php]func_get_args () [/url] Method:
- //Yes, parameter list is empty
- function foo () {
- //Get all array of incoming parameters
- $args = Func_get_args ();
- foreach ($args as $k = + $v) {
- echo "Arg". $k + 1). ": $v \ n";
- }
- }
- foo ();
- /* Nothing will output */
- foo (' Hello ');
- /* Output
- Arg1:hello
- */
- foo (' Hello ', ' world ', ' again ');
- /* Output
- Arg1:hello
- Arg2:world
- Arg3:again
- */
2, using Glob () Find files Most of the functions of PHP function name can literally understand its purpose, but when you see Glob (), you may not know what this is to do, in fact glob () and Scandir (), can be used to find files, see the following usage: PHP code
- Get all the files that are suffixed with 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 multiple suffix names: PHP code
- Fetch PHP files 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
- )
- */
You can also add the path: PHP code
- $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: PHP code
- $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 recycling mechanism is very powerful, you can also use PHP script to get the current memory usage, call Memory_get_usage () function to get memory usage of the period, call Memory_get_peak_usage () function to get the peak memory usage. The reference code is as follows: PHP code
- echo "Initial:". Memory_get_usage (). "Bytes \ n";
- /* Output
- initial:361400 bytes
- */
- Using memory
- for ($i = 0; $i < 100000; $i + +) {
- $array []= MD5 ($i);
- }
- Delete half of the memory
- 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";
- /* Output Peak
- peak:13687072 bytes
- */
4, Get CPU usage information get memory usage, can also use PHP getrusage () to get CPU usage, this method is not available under Windows. PHP code
- Print_r (Getrusage ());
- /* Output
- 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 very obscure, unless you know the CPU well. Here are some explanations:
- Ru_oublock: Block output operation
- Ru_inblock: Block input operation
- RU_MSGSND: Message Sent
- RU_MSGRCV: Message Received
- Ru_maxrss: Maximum resident set size
- RU_IXRSS: Total Shared memory size
- Ru_idrss: Total non-shared memory size
- Ru_minflt: Page Recycling
- Ru_majflt: Page Failure
- Ru_nsignals: Received Signal
- RU_NVCSW: Active Context Switch
- RU_NIVCSW: Passive Context Switch
- Ru_nswap: Swap Area
- 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)
To see how much CPU your script consumes, we need to look at the "user-state time" and "system kernel Time" values. Seconds and microseconds are provided separately, and you can divide the microsecond value by 1 million and add it to the second value to get the number of seconds that have fractional parts. PHP code
- 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 occupy the system time, we can take a look at the following example: PHP code
- Loop million times (busy)
- for ($i =0; $i <10000000; $i + +) {
- }
- $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:1.424592
- System time:0.004204
- */
This took about 14 seconds of CPU time, almost all of it was user time, because there was no system call. The system time is the time that the CPU spends executing kernel instructions on the system call. Here's an example: PHP code
- $start = Microtime (true);
- Keep calling microtime for about 3 seconds
- while (Microtime (True) – $start < 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);
- /* Prints
- User time:1.088171
- System time:1.675315
- */
We can see that the above example consumes more CPU. 5. Get system constants PHP provides very useful system constants that allow you to get the current line number (__line__), file (__file__), directory (__dir__), function name (__function__), class name (__class__), Method name (__ method__) and Namespace (__namespace__), much like the C language. We can assume that these things are mainly used for debugging, and when not necessarily, for example, can we use other files when we include them? __file__ (of course, you can also use __dir__ after PHP 5.3), here's an example. PHP code
- This was relative to the loaded script ' s path
- It may cause problems if running scripts from different directories
- Require_once (' config/database.php ');
- This is all relative to the this file ' s path
- No matter where it is included from
- Require_once (DirName (__file__). '/config/database.php ');
Here is the use of __line__ to output some debug information, which will help you debug the program: PHP code
- Some code
- ...
- My_debug ("Some debug message", __line__);
- /* Output
- Line 4:some Debug Message
- */
- Some more code
- ...
- My_debug ("Another debug Message", __line__);
- /* Output
- Line 11:another Debug Message
- */
- function My_debug ($msg, $line) {
- echo "line $line: $msg \ n";
- }
6. Generate a unique ID many friends use MD5 () to generate unique numbers, but MD5 () has several drawbacks: 1, unordered, resulting in degraded sorting performance in the database. 2, too long, need more storage space. In fact, PHP comes with a function to generate a unique ID, this function is Uniqid (). Here's how to use: PHP code
- Generate unique string
- Echo Uniqid ();
- /* Output
- 4bd67c947233e
- */
- Generate another unique string
- Echo Uniqid ();
- /* Output
- 4bd67c9472340
- */
The algorithm is generated according to the CPU time stamp, so in a similar time period, the ID of the first few are the same, it is convenient to sort the ID, if you want to better avoid repetition, you can prefix the ID, such as: PHP code
- Prefix
- echo uniqid (' Foo_ ');
- /* Output
- foo_4bd67d6cd8b8f
- */
- There's more entropy.
- Echo Uniqid (", true);
- /* Output
- 4bd67d6cd8b926.12135106
- */
- All have
- echo uniqid (' Bar_ ', true);
- /* Output
- bar_4bd67da367b650.43684647
- */
7, serialized PHP serialization function You may use more, also more common, when you need to save the data to the database or file is, you can use PHP serialize () and Unserialize () method to achieve serialization and deserialization, the code is as follows: PHP code
- A complex array
- $myvar = Array (
- ' Hello ',
- 42,
- Array (1, ' both '),
- ' Apple '
- );
- Serialization of
- $string = serialize ($myvar);
- Echo $string;
- /* Output
- A:4:{i:0;s:5: "Hello"; I:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3: "Both";} I:3;s:5: "Apple";}
- */
- Inverse order of the case
- $newvar = Unserialize ($string);
- Print_r ($newvar);
- /* Output
- Array
- (
- [0] = = Hello
- [1] = 42
- [2] = = Array
- (
- [0] = 1
- [1] =
- )
- [3] = Apple
- )
- */
How to serialize to JSON format, rest assured that PHP is ready for you, using PHP 5.2 or later users can use the Json_encode () and the Json_decode () function to implement the JSON format serialization, the code is as follows: PHP code
- A complex array
- $myvar = Array (
- ' Hello ',
- 42,
- Array (1, ' both '),
- ' Apple '
- );
- Convert to a string
- $string = Json_encode ($myvar);
- Echo $string;
- /* Prints
- ["Hello", 42,[1, "I"], "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] =
- )
- [3] = Apple
- )
- */
8, string compression when we talk about compression, we may think of file compression, in fact, the string can also be compressed. PHP provides the gzcompress () and Gzuncompress () Functions: PHP code
- $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 ";
- /* Size after output is compressed
- Compressed size:418
- */
- Unzip
- $original = gzuncompress ($compressed);
There are almost 50% compression ratios. You can also use the Gzencode () and Gzdecode () functions to compress without using a different compression algorithm. The above is the 8 development of the necessary PHP features, is not all very practical?
Source: 8 PHP features to develop
http://www.bkjia.com/PHPjc/735057.html www.bkjia.com true http://www.bkjia.com/PHPjc/735057.html techarticle developers who have done PHP development should be aware that PHP has a lot of built-in features, mastered them, can help you in the development of PHP more handy, this article will share 8 development necessary ...