doumi7854 2014-08-04 15:29
浏览 150
已采纳

如何使用eval()调用PHP上的函数?

How can I call functions on PHP using eval()? is it possible? I wanna do something like this

<?php

$function1 = 'echo';
$function2 = 'implode';
$arr = array('arg1', 'arg2');

eval("$function1 ($function2(', ', $arr));");

?>

Or call other functions with multiple params? Is eval() what I'm looking for? Many thanks!

  • 写回答

1条回答 默认 最新

  • dourun2990 2014-08-04 15:31
    关注

    You don't need to eval anything:

    $function1 = 'print';
    $function2 = 'implode';
    $arr = array('arg1', 'arg2');
    
    $function1($function2(', ', $arr));
    

    In fact, you can even use variable, variables:

    $foo = 'print';
    $bar = 'foo';
    
    $$bar('hello');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?