Home > Backend Development > PHP Tutorial > PHP可变函数

PHP可变函数

WBOY
Release: 2016-06-23 13:36:44
Original
1037 people have browsed it

可变函数是指同样的函数名称,其函数体可以动态改变。

使用方法:可通过把函数名当成变量,从而实现可变函数的功能。

下面看一例子:

 function add($a, $b){
        return $a + $b;
    }

    function sub($a, $b){
        return $a - $b;
    }

    $a = 10;
    $b = 2;

    $operation = 'add';
    echo " $a + $b = ".$operation($a, $b).'
';

    $operation = 'sub';
    echo " $a - $b = ".$operation($a, $b).'
';

?>

运行结果:


Related labels:
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