Home > Backend Development > PHP Tutorial > How to call a function in php when its name is a variable

How to call a function in php when its name is a variable

WBOY
Release: 2016-07-25 08:59:00
Original
1687 people have browsed it
  1. function fun()
  2. {
  3. echo 'fun';
  4. }
  5. $a = 'fun';
  6. $a();
  7. ?>
Copy code

The $a variable above is the fun() function. Calling $a() is the same as calling fun().

Another way. Call a function in PHP: mixed call_user_func ( callback $function [, mixed $parameter [, mixed $...]] ) mixed call_user_func_array ( callback $function, array $param_arr )

Example:

  1. //Function name as variable
  2. //by bbs.it-home.org
  3. function barber($type)
  4. {
  5. echo "You wanted a $type haircut, no problem" ;
  6. }
  7. call_user_func('barber', "mushroom");
  8. call_user_func('barber', "shave");
  9. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template