Home >Backend Development >PHP Tutorial >php dynamically creates functions at runtime

php dynamically creates functions at runtime

WBOY
WBOYOriginal
2016-07-25 08:43:31958browse

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. if (count($_POST) > 0) {
  2. $prepped = create_function('$a', 'return trim($_POST[$a]);');
  3. }
  4. elseif (count($_GET) > 0 ) {
  5. $prepped = create_function('$a', 'return strtoupper($_GET[$a]);');
  6. }
  7. else {
  8. $prepped = create_function('$a', 'return false;') ;
  9. }echo $prepped('file');
  10. ?>
Copy code

php


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
Previous article:php httpClient classNext article:php httpClient class