Several useful PHP Functions

Source: Internet
Author: User
Tags glob

Reprinted: http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2011/0708/8414_3.html

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.
Now let's take a look at a function with an indefinite parameter. is it used? Func_get_args () method

// Yes, the parameter list is empty
Function Foo (){
// Obtain the array of all input parameters
$ ARGs = func_get_args ();
Foreach ($ ARGs as $ K =>v v ){
Echo "Arg". ($ k + 1). ": $ 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

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.

// Obtain all files suffixed with PHP
$ Files = glob ('*. php ');
Print_r ($ files );

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.

4. CPU usage information

Use? The getrusage () function allows you to know the CPU usage. Note that this function is not available in windows.

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 people use MD5 () to generate a unique ID. In fact, there is one in PHP called? The uniqid () function is used to do this.

You may notice that the first few digits of the generated ID are the same, because the generator depends on the system time, which is actually a very good function, because you can easily sort your IDs. This MD5 cannot be achieved.

In addition, the generated ID is shorter than that generated by MD5, which saves a lot of space.

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 ():

This is a native function of PHP. However, JSON is becoming more and more popular today, so after php5.2, PHP began to support JSON. You can use json_encode () and json_decode () functions.

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

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.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.