Summary of function instances that are powerful but rarely used in PHP,

Source: Internet
Author: User

Summary of function instances that are powerful but rarely used in PHP,

This example describes functions that are powerful but rarely used in PHP. We will share this with you for your reference. The details are as follows:

Call_user_func_array-calls a function as an array.
Call_user_func-call an existing Function
Create_function-create a function
Func_get_arg-get the value of a parameter in the function
Func_get_args-Get all the parameters of the function and form an array
Func_num_args-get the number of parameters of a function
Function_exists-determine whether a function exists
Get_defined_functions-Get existing function Information
Register_shutdown_function-registers a function that runs after a page is loaded.
Register_tick_function-registers a function called as required
Unregister_tick_function-cancels a function called as required

Get_defined_functions can get all PHP functions and custom functions:

<? Phpfunction a () {}$ B = get_defined_functions (); print_r ($ B); // more than 1000 defined functions may be displayed. :)?>

The function_exists function determines whether a function exists (either a PHP function or a custom function ).

<? Phpif (function_exists ('A') {echo "yes" ;}else {echo "no" ;}function a () {}// display yes?>

The call_user_func function is similar to a special method for calling a function. The usage is as follows:

<? Phpfunction a ($ B, $ c) {echo $ B; echo $ c;} call_user_func ('A', "111", "222"); call_user_func ('A ', "333", "444"); // display 111 222 333 444?>

The internal method of calling the class is strange. The method actually uses an array and does not know how developers think about it. Of course, the new method is saved, which is also innovative:

<? Phpclass a {function B ($ c) {echo $ c ;}} call_user_func (array ("a", "B"), "111"); // display 111?>

The call_user_func_array function is similar to call_user_func, but the parameter is passed in another way to make the parameter structure clearer:

<? Phpfunction a ($ B, $ c) {echo $ B; echo $ c;} call_user_func_array ('A', array ("111", "222 ")); // display 111 222?>

Call_user_func and call_user_func_array Functions Support reference, which makes them more functional than normal function calls:

<? Phpfunction a (& $ B) {$ B ++;} $ c = 0; call_user_func ('A', & $ c); echo $ c; // display 1call_user_func_array ('A', array (& $ c); echo $ c; // display 2?>

The func_num_args function can obtain the number of parameters received by the function:

<? Phpfunction a () {echo func_num_args () ;}a (111,222,333); // display 3?>

The func_get_arg function can obtain the value of a passed parameter. In the following example, the function does not specify which parameters will be accepted, you can use func_get_arg to obtain additional parameters:

<? Phpfunction a () {echo func_get_arg (1) ;}a (111,222,333); // display 222?>

The function func_get_args is similar to func_get_arg in that all parameters are called as Arrays:

<? Phpfunction a () {$ numargs = func_num_args (); $ B = func_get_args (); for ($ I = 0; $ I <$ numargs; $ I ++) {echo $ B [$ I] ;}} a (111,222,333); // display 111 222 333?>

The create_function can be used to create an anonymous function (the function name is named lambda_1 and lambda_2 by default by PHP). It looks odd, but the form is strange, note that the statements in the second parameter must be separated:

<? Php $ newfunc = create_function ('$ a, $ B', 'Return $ a + $ B; '); echo $ newfunc (2, 3 ); // display lambda_1 5?>

The register_shutdown_function can be used to register a function that runs after the page is loaded (the function is somewhat like a cache). The register_shutdown_function can also be used to call internal methods of the class like the call_user_func function:

<? Phpfunction a () {echo 222;} echo 111; register_shutdown_function ('A'); // display 111 222?>
<? Phpclass a {function B ($ c) {echo $ c ;}} register_shutdown_function (array ('A', 'B'), '000000'); // display 111?>

The register_tick_function function and the unregister_tick_function function must be used together with the declare process control mechanism. First, let's take a look at declare and tick:

declare (directive){statement}

Tick is an event that occurs when the interpreter executes N low-level statements in the declare code segment. The value of N is specified by ticks = N in the directive section of declare. The events that occur in each tick are specified by register_tick_function. Example:

<?function foo($str) {static $i = 0;print "$str: $i<br>";$i++;}register_tick_function("foo", "count");declare (ticks = 6) {for($i=0; $i<20; $i++) {echo "$i<br>";}}?>

In the declare (ticks = N) {statement} process control in this example, run the foo () function once every six lines of code (ticks = 6, by changing the ticks value, we can get different running results.

Related Article

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: 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.