PHP: Variable in a function name

后端 未结 7 1872
有刺的猬
有刺的猬 2020-12-09 16:57

I want to trigger a function based on a variable.

function sound_dog() { return \'woof\'; }
function sound_cow() { return \'moo\'; }

$animal = \'cow\';
print soun         


        
7条回答
  •  一生所求
    2020-12-09 17:36

    For PHP >= 7 you can use this way:

    function sound_dog() { return 'woof'; }
    function sound_cow() { return 'moo'; }
    
    $animal = 'cow';
    print ('sound_' . $animal)();
    

提交回复
热议问题