Home  >  Article  >  Backend Development  >  Some powerful but rarely used functions in PHP

Some powerful but rarely used functions in PHP

*文
*文Original
2018-01-02 15:09:021004browse

This article mainly introduces the powerful but rarely used functions in PHP, and summarizes and analyzes several very practical functions in PHP in the form of examples, including function calling, registration, calling, judgment and other operating skills. Friends in need can refer to it. I hope it will be helpful to everyone.

The details are as follows:

call_user_func_array — let the parameters be called an array in the form of 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 — Register a function that runs after the page is loaded.
register_tick_function — Register a function that is called on request.
unregister_tick_function — Unregister a function that is called on request.

get_defined_functions can get all PHP functions and custom functions:

<?php
function a(){}
$b = get_defined_functions();
print_r($b);
//也许会显示1000多个已定义了的函数:)
?>

The function_exists function determines whether a function exists (it can be a PHP function or a custom function).

<?php
if (function_exists(&#39;a&#39;)) {
  echo "yes";
} else {
  echo "no";
}
function a(){}
// 显示 yes
?>

The call_user_func function is similar to a special method of calling a function. The method of use is as follows:

<?php
function a($b,$c)
{
  echo $b;
  echo $c;
}
call_user_func(&#39;a&#39;, "111","222");
call_user_func(&#39;a&#39;, "333","444");
//显示 111 222 333 444
?>

It is strange to call the method inside the class. It actually uses an array. I don’t know who the developer is. How to consider, of course, new is omitted, which is also full of novelty:

<?php
class a {
  function b($c)
  {
    echo $c;
  }
}
call_user_func(array("a", "b"),"111");
//显示 111
?>

The call_user_func_array function is very similar to call_user_func, except that the parameters are passed in a different way to make the parameter structure clearer:

<?php
function a($b, $c)
{
  echo $b;
  echo $c;
}
call_user_func_array(&#39;a&#39;, array("111", "222"));
//显示 111 222
?>

Both the call_user_func function and the call_user_func_array function support references, which makes them more functionally consistent with ordinary function calls:

<?php
function a(&$b)
{
  $b++;
}
$c = 0;
call_user_func(&#39;a&#39;, &$c);
echo $c;//显示 1
call_user_func_array(&#39;a&#39;, array(&$c));
echo $c;//显示 2
?>

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

<?php
function a()
{
  echo func_num_args();
}
a(111, 222, 333);
//显示 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 also obtain additional parameters by using func_get_arg:

<?php
function a()
{
   echo func_get_arg(1);
}
a (111, 222, 333);
//显示 222
?>

func_get_args function is very similar to func_get_arg. It calls all parameters as an array:

<?php
function a()
{
  $numargs = func_num_args();
  $b = func_get_args();
  for ($i = 0; $i < $numargs; $i++) {
    echo $b[$i];
  }
}
a(111, 222, 333);
//显示 111 222 333
?>

create_function function can create an anonymous function (the function name is lambda_1, lambda_2 by PHP by default), the appearance is relatively similar Weird, but the form is quite peculiar. Please note that the statements in the second parameter must be separated by ";":

<?php
$newfunc = create_function(&#39;$a,$b&#39;, &#39;return $a + $b;&#39;);
echo $newfunc;
echo $newfunc(2, 3);
//显示 lambda_1 5
?>

register_shutdown_function function can register a function that runs after the page is loaded (the function is a bit like caching) , register_shutdown_function can also be used to call internal methods of the class like the call_user_func function:

<?php
function a() {
  echo 222 ;
}
echo 111;
register_shutdown_function(&#39;a&#39;);
//显示 111 222
?>
<?php
class a
{
  function b ($c)
  {
   echo $c;
  }
}
register_shutdown_function (array (&#39;a&#39;, &#39;b&#39;), &#39;111&#39;);
//显示 111
?>

The register_tick_function function and the unregister_tick_function function must be combined with the declare process control mechanism, so first understand declare and tick:

declare (directive){
statement
}

Tick is an event that occurs every time the interpreter executes N low-level statements in the declare section. The value of N is specified with ticks=N in the directive part of declare. The events that occur in each tick are specified by register_tick_function(). An example is 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 the declare(ticks = N){statement} process control in this example, every time 6 lines of code (ticks = 6) are executed, the foo() function needs to be run once. By changing With the value of ticks, we can get different running results.

Related recommendations:

PHP Function Syntax Introduction 1_PHP Tutorial

PHP A small comparison of function execution efficiency_PHP tutorial

php Function usage method and function definition method_PHP tutorial

The above is the detailed content of Some powerful but rarely used functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn