函数处理 函数 业 ,精于勤 荒于嬉.
- 函数处理 函数 call_user_func_array 调用回调函数,并把一个数组参数作为回调函数的参数
-
发表日期:2021-07-01 08:57:15 | 来源: | 分类:函数处理 函数
-
示例1
01
<?php
02
function
foobar(
$arg
,
$arg2
) {
03
echo
__FUNCTION__
,
" got $arg and $arg2\n"
;
04
}
05
class
foo {
06
function
bar(
$arg
,
$arg2
) {
07
echo
__METHOD__
,
" got $arg and $arg2\n"
;
08
}
09
}
10
// Call the foobar() function with 2 argumentscall_user_func_array("foobar", array("one", "two"));
11
// Call the $foo->bar() method with 2 arguments$foo = new foo;
12
call_user_func_array(
array
(
$foo
,
"bar"
),
array
(
"three"
,
"four"
));
13
?>
示例2
01
<?php
02
namespace Foobar;
03
class
Foo {
04
static
public
function
test(
$name
) {
05
print "Hello {
06
$name
}
07
!\n";
08
}
09
}
10
// As of PHP 5.3.0call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));
11
// As of PHP 5.3.0call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));
12
?>
示例3
1
<?php
2
$func
=
function
(
$arg1
,
$arg2
) {
3
return
$arg1
*
$arg2
;
4
}
5
;
6
var_dump(call_user_func_array(
$func
,
array
(2, 4)));
7
/* As of PHP 5.3.0 */
8
?>
示例4
1
<?php
2
function
mega(&
$a
){
3
$a
= 55;
4
echo
"function mega \$a=$a\n"
;
5
}
6
$bar
= 77;
7
call_user_func_array(
'mega'
,
array
(&
$bar
));
8
echo
"global \$bar=$bar\n"
;
9
?>
- 函数处理 函数 call_user_func 把第一个参数作为回调函数调用
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
示例1
01
<?php
02
error_reporting
(E_ALL);
03
function
increment(&
$var
){
04
$var
++;
05
}
06
$a
= 0;
07
call_user_func(
'increment'
,
$a
);
08
echo
$a
.
"\n"
;
09
call_user_func_array(
'increment'
,
array
(&
$a
));
10
// You can use this instead before PHP 5.3echo $a."\n";
11
?>
示例2
1
<?php
2
function
barber(
$type
){
3
echo
"You wanted a $type haircut, no problem\n"
;
4
}
5
call_user_func(
'barber'
,
"mushroom"
);
6
call_user_func(
'barber'
,
"shave"
);
7
?>
示例3
01
<?php
02
namespace Foobar;
03
class
Foo {
04
static
public
function
test() {
05
print
"Hello world!\n"
;
06
}
07
}
08
call_user_func(__NAMESPACE__ .
'\Foo::test'
);
09
// As of PHP 5.3.0call_user_func(array(__NAMESPACE__ .'\Foo', 'test'));
10
// As of PHP 5.3.0?>
示例4
01
<?php
02
class
myclass {
03
static
function
say_hello() {
04
echo
"Hello!\n"
;
05
}
06
}
07
$classname
=
"myclass"
;
08
call_user_func(
array
(
$classname
,
'say_hello'
));
09
call_user_func(
$classname
.
'::say_hello'
);
10
// As of 5.2.3$myobject = new myclass();
11
call_user_func(
array
(
$myobject
,
'say_hello'
));
12
?>
示例5
1
<?php
2
call_user_func(
function
(
$arg
) {
3
print
"[$arg]\n"
;
4
}
5
,
'test'
);
6
/* As of PHP 5.3.0 */
7
?>
- 函数处理 函数 create_function Create an anonymous (lambda-style) function
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
示例1
1
<?php
2
$newfunc
= create_function(
'$a,$b'
, '
return
"ln($a) + ln($b) = "
. log(
$a
*
$b
);
3
');
4
echo
"New anonymous function: $newfunc\n"
;
5
echo
$newfunc
(2, M_E) .
"\n"
;
6
// outputs// New anonymous function: lambda_1// ln(2) + ln(2.718281828459) = 1.6931471805599?>
示例2
01
<?php
02
function
process(
$var1
,
$var2
,
$farr
){
03
foreach
(
$farr
as
$f
) {
04
echo
$f
(
$var1
,
$var2
) .
"\n"
;
05
}
06
}
07
// create a bunch of math functions$f1 = 'if ($a >=0) {
08
return
"b*a^2 = "
.
$b
*sqrt(
$a
);
09
}
10
else
{
11
return
false;
12
}
13
';
14
$f2
= "
return
\"min(b^2+a, a^2,b) = \".min(\
$a
*\
$a
+\
$b
,\
$b
*\
$b
+\
$a
);
15
";
16
$f3
= '
if
(
$a
> 0 &&
$b
!= 0) {
17
return
"ln(a)/b = "
.log(
$a
)/
$b
;
18
}
19
else
{
20
return
false;
21
}
22
';
23
$farr
=
array
( create_function(
'$x,$y'
, '
return
"some trig: "
.(sin(
$x
) +
$x
*
cos
(
$y
));
24
'), create_function('
$x
,
$y
', '
return
"a hypotenuse: "
.sqrt(
$x
*
$x
+
$y
*
$y
);
25
'), create_function('
$a
,
$b
', $f1), create_function('
$a
,
$b
', $f2), create_function('
$a
,
$b
',
$f3
) );
26
echo
"\nUsing the first array of anonymous functions\n"
;
27
echo
"parameters: 2.3445, M_PI\n"
;
28
process(2.3445, M_PI,
$farr
);
29
// now make a bunch of string processing functions$garr = array( create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '. 'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";
30
'), create_function('
$a
,
$b
', '
;
31
return
"CRCs: "
. crc32(
$a
) .
", "
.crc32(
$b
);
32
'), create_function('
$a
,
$b
', '
;
33
return
"similar(a,b) = "
. similar_text(
$a
,
$b
, &
$p
) .
"($p%)"
;
34
') );
35
echo
"\nUsing the second array of anonymous functions\n"
;
36
process(
"Twas brilling and the slithy toves"
,
"Twas the night"
,
$garr
);
37
?>
示例3
1
<?php
2
$av
=
array
(
"the "
,
"a "
,
"that "
,
"this "
);
3
array_walk
(
$av
, create_function(
'&$v,$k'
, '
$v
=
$v
.
"mango"
;
4
'));
5
print_r(
$av
);
6
?>
示例4
1
<?php
2
$sv
=
array
(
"small"
,
"larger"
,
"a big string"
,
"it is a string thing"
);
3
print_r(
$sv
);
4
?>
示例5
1
<?php
2
usort(
$sv
, create_function(
'$a,$b'
,'
return
strlen
(
$b
) -
strlen
(
$a
);
3
'));
4
print_r(
$sv
);
5
?>
- 函数处理 函数 forward_static_call_array Call a static method and pass the arguments as array
-
发表日期:2021-07-01 08:57:15 | 来源: | 分类:函数处理 函数
-
示例1
01
<?php
02
class
A{
03
const
NAME =
'A'
;
04
public
static
function
test() {
05
$args
= func_get_args();
06
echo
static
::NAME,
" "
.join(
','
,
$args
).
" \n"
;
07
}
08
}
09
class
B
extends
A{
10
const
NAME =
'B'
;
11
public
static
function
test() {
12
echo
self::NAME,
"\n"
;
13
forward_static_call_array(
array
(
'A'
,
'test'
),
array
(
'more'
,
'args'
));
14
forward_static_call_array(
'test'
,
array
(
'other'
,
'args'
));
15
}
16
}
17
B::test(
'foo'
);
18
function
test() {
19
$args
= func_get_args();
20
echo
"C "
.join(
','
,
$args
).
" \n"
;
21
}
22
?>
- 函数处理 函数 forward_static_call Call a static method
-
发表日期:2021-07-01 08:57:15 | 来源: | 分类:函数处理 函数
-
示例1
01
<?php
02
class
A{
03
const
NAME =
'A'
;
04
public
static
function
test() {
05
$args
= func_get_args();
06
echo
static
::NAME,
" "
.join(
','
,
$args
).
" \n"
;
07
}
08
}
09
class
B
extends
A{
10
const
NAME =
'B'
;
11
public
static
function
test() {
12
echo
self::NAME,
"\n"
;
13
forward_static_call(
array
(
'A'
,
'test'
),
'more'
,
'args'
);
14
forward_static_call(
'test'
,
'other'
,
'args'
);
15
}
16
}
17
B::test(
'foo'
);
18
function
test() {
19
$args
= func_get_args();
20
echo
"C "
.join(
','
,
$args
).
" \n"
;
21
}
22
?>
- 函数处理 函数 func_get_arg 返回参数列表的某一项
-
发表日期:2021-07-01 08:57:15 | 来源: | 分类:函数处理 函数
-
示例1
01
<?php
02
function
foo(){
03
$numargs
= func_num_args();
04
echo
"Number of arguments: $numargs<br />\n"
;
05
if
(
$numargs
>= 2) {
06
echo
"Second argument is: "
. func_get_arg(1) .
"<br />\n"
;
07
}
08
}
09
foo (1, 2, 3);
10
?>
示例2
1
test.php<?php
2
function
foo() {
3
include
'./fga.inc'
;
4
}
5
foo(
'First arg'
,
'Second arg'
);
6
?>fga.inc<?php
$arg
= func_get_arg(1);
7
var_export(
$arg
);
8
?>
示例3
01
<?php
02
function
byVal(
$arg
) {
03
echo
'As passed : '
, var_export(func_get_arg(0)), PHP_EOL;
04
$arg
=
'baz'
;
05
echo
'After change : '
, var_export(func_get_arg(0)), PHP_EOL;
06
}
07
function
byRef(&
$arg
) {
08
echo
'As passed : '
, var_export(func_get_arg(0)), PHP_EOL;
09
$arg
=
'baz'
;
10
echo
'After change : '
, var_export(func_get_arg(0)), PHP_EOL;
11
}
12
$arg
=
'bar'
;
13
byVal(
$arg
);
14
byRef(
$arg
);
15
?>
- 函数处理 函数 func_get_args 返回一个包含函数参数列表的数组
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
示例1
01
<?php
02
function
foo(){
03
$numargs
= func_num_args();
04
echo
"Number of arguments: $numargs<br />\n"
;
05
if
(
$numargs
>= 2) {
06
echo
"Second argument is: "
. func_get_arg(1) .
"<br />\n"
;
07
}
08
$arg_list
= func_get_args();
09
for
(
$i
= 0;
10
$i
<
$numargs
;
11
$i
++) {
12
echo
"Argument $i is: "
.
$arg_list
[
$i
] .
"<br />\n"
;
13
}
14
}
15
foo(1, 2, 3);
16
?>
示例2
1
test.php<?php
2
function
foo() {
3
include
'./fga.inc'
;
4
}
5
foo(
'First arg'
,
'Second arg'
);
6
?>fga.inc<?php
$args
= func_get_args();
7
var_export(
$args
);
8
?>
示例3
01
<?php
02
function
byVal(
$arg
) {
03
echo
'As passed : '
, var_export(func_get_args()), PHP_EOL;
04
$arg
=
'baz'
;
05
echo
'After change : '
, var_export(func_get_args()), PHP_EOL;
06
}
07
function
byRef(&
$arg
) {
08
echo
'As passed : '
, var_export(func_get_args()), PHP_EOL;
09
$arg
=
'baz'
;
10
echo
'After change : '
, var_export(func_get_args()), PHP_EOL;
11
}
12
$arg
=
'bar'
;
13
byVal(
$arg
);
14
byRef(
$arg
);
15
?>
- 函数处理 函数 func_num_args Returns the number of arguments passed to the function
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
示例1
1
<?php
2
function
foo(){
3
$numargs
= func_num_args();
4
echo
"Number of arguments: $numargs\n"
;
5
}
6
foo(1, 2, 3);
7
?>
示例2
1
test.php<?php
2
function
foo() {
3
include
'./fna.php'
;
4
}
5
foo(
'First arg'
,
'Second arg'
);
6
?>fna.php<?php
$num_args
= func_num_args();
7
var_export(
$num_args
);
8
?>
- 函数处理 函数 function_exists 如果给定的函数已经被定义就返回 true
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
示例1
1
<?php
2
if
(function_exists(
'imap_open'
)) {
3
echo
"IMAP functions are available.<br />\n"
;
4
}
5
else
{
6
echo
"IMAP functions are not available.<br />\n"
;
7
}
8
?>
- 函数处理 函数 get_defined_functions 返回所有已定义函数的数组
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
示例1
1
<?php
2
function
myrow(
$id
,
$data
){
3
return
"<tr><th>$id</th><td>$data</td></tr>\n"
;
4
}
5
$arr
= get_defined_functions();
6
print_r(
$arr
);
7
?>
- 函数处理 函数 register_shutdown_function 注册一个会在php中止时执行的函数
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
示例1
1
<?php
2
function
shutdown(){
3
// This is our shutdown function, in // here we can do any last operations // before the script is complete. echo 'Script executed with success', PHP_EOL;
4
}
5
register_shutdown_function(
'shutdown'
);
6
?>
- 函数处理 函数 register_tick_function Register a function for execution on each tick
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
示例1
1
<?php
2
declare
(ticks=1);
3
// using a function as the callbackregister_tick_function('my_function', true);
4
// using an object->method$object = new my_class();
5
register_tick_function(
array
(&
$object
,
'my_method'
), true);
6
?>
- 函数处理 函数 unregister_tick_function De-register a function for execution on each tick
-
发表日期:2021-07-01 08:57:16 | 来源: | 分类:函数处理 函数
-
unregister_tick_function
(PHP 4 >= 4.0.3, PHP 5, PHP 7, PHP 8)
unregister_tick_function — De-register a function for execution on each tick
说明
unregister_tick_function(string$function_name
): voidDe-registers the function named by
function_name
so it is no longer executed when a tick is called.参数
-
function_name
-
The function name, as a string.
返回值
没有返回值。
-
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)