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条)

报告相同问题?

悬赏问题

  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题