dsfs504545 2017-04-23 10:30
浏览 39
已采纳

如何在变量中使用变量来定义php函数,然后回显它

I am trying to use variable in variable function, then to do some calculations and later use that variable and print out answer:

    <?php

$a($b)=function() {

if ($b == 10 ) {
    return 10 ;
}else{
    return 20 ;

     }
}

$a(10);
echo $a(10);

?>

I am getting error:

Fatal error: Can't use function return value in write context

  • 写回答

2条回答 默认 最新

  • dounayan3643 2017-04-23 10:32
    关注

    Here $a($b) means invoking a function instead of declaration. you should define $a and pass $b as a variable to the function.

    Change this to:

    $a($b)=function() {
    

    This:

    $a=function($b) {
    

    Whole PHPcode Try this code snippet here

    <?php
    
    ini_set('display_errors', 1);
    
    $a = function($b)
    {
    
        if ($b == 10)
        {
            return 10;
        } else
        {
            return 20;
        }
    };
    
    echo $a(10);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?