如何在变量中使用变量来定义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

我试图在变量函数中使用变量,然后进行一些计算,然后使用该变量并打印出答案 : p>

 &lt;?php 
 
 $ a($ b)= function(){
 
if($ b == 10){
 return 10  ; 
}其他{
返回20; 
 
} 
} 
 
 $ a(10); 
echo $ a(10); 
 
?&gt; 
  code>   pre> 
 
 

我收到错误: p>

致命错误:无法在写上下文中使用函数返回值 p> \ n blockquote> div>

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);
?>

Change following line

$a($b)=function() {

to

$a=function($b) {