Home >Backend Development >PHP Tutorial >How to dynamically create functions when PHP is running_PHP Tutorial

How to dynamically create functions when PHP is running_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:03:37880browse

How to dynamically create functions when PHP is running

This article mainly introduces the method of dynamically creating functions when PHP is running, involving the usage skills of PHP process statements and create_function functions. Need Friends can refer to it

The example in this article describes the method of dynamically creating functions when PHP is running. Share it with everyone for your reference. The specific analysis is as follows:

General language functions must be defined at runtime, and PHP supports dynamic creation of functions at runtime. The following is a simple example to create a function $a according to different conditions during exercise

?

1

2

3

4

5

6

7

8

9

10

11

if (count($_POST) > 0) {

$prepped = create_function('$a', 'return trim($_POST[$a]);');

}

elseif (count($_GET) > 0) {

$prepped = create_function('$a', 'return strtoupper($_GET[$a]);');

}

else {

$prepped = create_function('$a', 'return false;');

}echo $prepped('file');

?>

1 2

3

4 5

67 8 9 10 11
if (count($_POST) > 0) {
$prepped = create_function('$a', 'return trim($_POST[$a]);'); } elseif (count($_GET) > 0) { $prepped = create_function('$a', 'return strtoupper($_GET[$a]);'); } else { $prepped = create_function('$a', 'return false;'); }echo $prepped('file'); ?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/968663.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/968663.htmlTechArticleHow to dynamically create functions when PHP is running. This article mainly introduces how to dynamically create functions when PHP is running, involving Tips on using php process statements and create_function functions, friends in need...
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