A powerful but rarely used function instance in PHP summary _php tips

Source: Internet
Author: User

This example describes a powerful but rarely used function in PHP. Share to everyone for your reference, specific as follows:

Call_user_func_array-lets the argument call a function as an array
Call_user_func-calls a function that exists
create_function-Create a function
Func_get_arg-gets the value of a parameter in a function
Func_get_args-gets all the parameters of the function and makes up an array
Func_num_args-gets the number of arguments for a function
function_exists-determines whether a function exists
get_defined_functions-Get Existing function information
register_shutdown_function-registers a function that runs after a page load completes
register_tick_function-registers a function called on demand
unregister_tick_function-cancels a function called on demand

Get_defined_functions can get all of the PHP functions and custom functions:

<?php
function 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 as a PHP function or as a custom function).

<?php
if (function_exists (' a ')) {
  echo "yes";
} else {
  echo "no";
}
function A () {}
//show Yes
?>

The Call_user_func function is similar to a particular method of calling a function, using the following method:

<?php
function A ($b, $c)
{
  echo $b;
  echo $c;
}
Call_user_func (' A ', "a", "222");
Call_user_func (' A ', "333", "444");
Show 222 333 444
?>

Call the internal method of the class is strange, incredibly with array, do not know how to consider the developer, of course, save the new, but also full of innovative:

<?php
Class A {
  function B ($c)
  {
    echo $c;
  }
}
Call_user_func (Array ("A", "B"), "a");
Show
?>

The Call_user_func_array function is very similar to the call_user_func, except that it passes the argument in a different way, making the parameter structure clearer:

<?php
function A ($b, $c)
{
  echo $b;
  echo $c;
}
Call_user_func_array (' A ', Array ("A", "222"));
Show 222
?>

Both the Call_user_func function and the Call_user_func_array function support references, which tend to be more functional consistent with normal function calls:

<?php
function A (& $b)
{
  $b + +;
}
$c = 0;
Call_user_func (' A ', & $c);
echo $c//Display 1
call_user_func_array (' A ', Array (& $c));
echo $c//Show 2
?>

The Func_num_args function can get the number of arguments that a function receives:

<?php
function A ()
{
  echo Func_num_args ();
}
A (MR, 222, 333);
Show 3
?>

The FUNC_GET_ARG function can fetch a value that passes over the parameter, in the following example, in the function does not specify which parameters are accepted, the use of Func_get_arg can also obtain additional parameters:

<?php
function A ()
{
   echo func_get_arg (1);
}
A (MR, 222, 333);
Show 222
?>

The functions of the Func_get_args function are very similar to Func_get_arg, which is to call all parameters as arrays:

<?php
function A ()
{
  $numargs = Func_num_args ();
  $b = Func_get_args ();
  for ($i = 0; $i < $numargs; $i + +) {
    echo $b [$i];
  }
}
A (MR, 222, 333);
Show 222 333
?>

Create_function function can create an anonymous function (the name of the letter by PHP default is lambda_1,lambda_2), look more bizarre, but the form is more peculiar, to note that the second parameter of the statement to have ";" Separated:

<?php
$newfunc = create_function (' $a, $b ', ' return $a + $b; ');
echo $newfunc;
Echo $newfunc (2, 3);
Show Lambda_1 5
?>

The Register_shutdown_function function can register a function that runs after the page load completes (functions are a bit like caching), and register_shutdown_function can also be like Call_user_ Func functions as a call to an internal method of a class:

<?php
function A () {
  echo 222;
}
echo;
Register_shutdown_function (' a ');
Show 222
?>

<?php
Class A
{
  function B ($c)
  {
   echo $c;
  }
}
Register_shutdown_function (Array (' A ', ' B '), ' a ');
Show
?>

The Register_tick_function function and the unregister_tick_function function must be combined with the declare process control mechanism to understand declare and tick first:

DECLARE (Directive) {
Statement
}

Tick is an event that occurs every time an interpreter executes N low-level statements in a declare code snippet. The value of N is specified by ticks=n in the directive portion of the declare. The events that occur in each tick are specified by Register_tick_function (). Examples are as follows:

?
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 this example declare (ticks = N) {statement} Process control, each execution of 6 lines (ticks = 6) code, you need to run the Foo () function, by changing the value of ticks, we can get different results of the operation.

More about PHP Interested readers can view the site topics: "PHP commonly used functions and skills summary", "Php+mysql Database operation Introduction Tutorial", "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Programming Program", " PHP Network Programming skills Summary, "PHP array Operation techniques Encyclopedia", "PHP string (String) Usage summary" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.