Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将函数存储到Php中的变量_Php_Function_Variables - Fatal编程技术网

将函数存储到Php中的变量

将函数存储到Php中的变量,php,function,variables,Php,Function,Variables,我想在变量中存储一个函数,可以吗? 让我们看看这个例子 <?php $tot_prezzo = 200; $x = 75; $y = 25; function addition() { echo $GLOBALS['imponibile'] = $GLOBALS['tot_prezzo'] + $GLOBALS['x']; } $func = 'addition'; $func(); ?> 如何将“$func()”存储在变量中? 我试过使用->$func\

我想在变量中存储一个函数,可以吗? 让我们看看这个例子

<?php 

$tot_prezzo = 200;
$x = 75;
$y = 25;

function addition() {
     echo $GLOBALS['imponibile'] = $GLOBALS['tot_prezzo'] + $GLOBALS['x'];
}

$func = 'addition';
$func();


?>

如何将“$func()”存储在变量中? 我试过使用->$func\u var=$func(),但不起作用。
我错过了什么

谢谢大家!

这称为“Lambda函数”,可以这样使用:

$addition = function($a, $b) {
    return $a+$b;
};

echo $addition(1,2); //3

要使用函数中的值,只需返回它即可

$tot_prezzo = 200;
$x = 75;
$y = 25;

function addition($tot_prezzo,$x,$y) {

    $imponibile = $tot_prezzo + $x;

    return $imponibile;
}

$result = addition($tot_prezzo,$x,$y);

print $result;

请注意使用参数而不是
$GLOBALS
,而且您没有使用
$y
变量。

您不必这样做,因为您可以在任何地方调用
addition()
。您将保存什么?您是否在函数中查找sum的值?我想在js脚本中使用函数的结果,以便将php变量转换为js变量。这样做正确吗:
var func=