Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.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_Echo - Fatal编程技术网

Php 如何从函数内部获取值?

Php 如何从函数内部获取值?,php,echo,Php,Echo,是否可以在函数内部获取这些值,并在函数外部使用这些值 这是我的密码: <? function expenditure () { $totalexpenditure = $sum1 + $sum2; } function income () { totalincome = $sum1 + $sum2; } $profit = $totalincome - $totalexpenditure; ?> 现在我的问题是

是否可以在函数内部获取这些值,并在函数外部使用这些值 这是我的密码:

   <?
    function expenditure () {
    $totalexpenditure = $sum1 + $sum2;
    }
    function income () {
    totalincome = $sum1 + $sum2;
    }
    $profit = $totalincome - $totalexpenditure;
   ?>

现在我的问题是,我怎样才能得到总收入和总支出的价值? 我正在学习php,这在php中是很新的,所以请帮帮我。


<?
function expenditure ($sum1, $sum2) {
    $totalexpenditure = $sum1 + $sum2;
    return $totalexpenditure;
}

function income ($sum1, $sum2) {
    $totalincome = $sum1 + $sum2;
    return $totalincome;
}

$profit = income ($sum1, $sum2) - expenditure($sum1, $sum2) ;
?>

您的代码错误,因为:

  • 函数中的变量没有赋值(您最好通过函数参数赋值,但另一个可行但错误的解决方案是将其设为全局变量)
  • 在给出的示例中,
    $profit
    将始终为
    0
    (零)
解决方案有三个:

解决方案1:

然后你可以这样使用它:

$profit = income(10, 200) - expenditure(20,18);
$my_finances = new Finances();
$my_finances->addExpense(20)->addExpense(18)->addIncome(10)->addIncome(10);
$profit = $my_finances->getProfit();
$sum1 = 10;
$sum2 = 200;
$expenditure = expenditure();
$sum1 = 20;
$sum2 = 30;
$income = income();
$profit = $income - $expenditure;
第二种解决方案:

然后你可以这样使用它:

$profit = income(10, 200) - expenditure(20,18);
$my_finances = new Finances();
$my_finances->addExpense(20)->addExpense(18)->addIncome(10)->addIncome(10);
$profit = $my_finances->getProfit();
$sum1 = 10;
$sum2 = 200;
$expenditure = expenditure();
$sum1 = 20;
$sum2 = 30;
$income = income();
$profit = $income - $expenditure;
解决方案3:(避免使用!)

然后你就这样使用它:

$profit = income(10, 200) - expenditure(20,18);
$my_finances = new Finances();
$my_finances->addExpense(20)->addExpense(18)->addIncome(10)->addIncome(10);
$profit = $my_finances->getProfit();
$sum1 = 10;
$sum2 = 200;
$expenditure = expenditure();
$sum1 = 20;
$sum2 = 30;
$income = income();
$profit = $income - $expenditure;

我希望您明白,为什么解决方案3是一个如此糟糕的主意(因为通常使用
全局
变量将某些内容传递给函数是一个糟糕的主意)。

这涉及到另一个问题,您可能会在稍后阶段面临。如果您想在一个函数中传递两个变量并更改它们的值,该怎么办

$var1 = 22;
$var2 = 15;

function multi2(&$x, &$y){
    $x = $x * 2;
    $y = $y * 2;
}

multi2($var1, $var2);
print $var1 . ", " . $var2;
您将得到这个输出

44, 30
$x
$y
参数本身不是变量,而是对传递的变量的引用(由
&
定义),如果需要在内部更改外部变量的值,这将非常有用


链接以了解更多内容

我必须缺少一些内容,为什么不直接从函数返回totalincome和TotalExpense?您知道吗,使用函数时:1)您的
$profit
将等于零,除非2)未为
$sum1
$sum2
赋值,如果不将它们设置为全局,则会产生另一个问题?请选择第二个答案作为答案?不,这是不正确的-
$sum1
$sum2
未定义。但这不是问题所在,他在使用返回语句时遇到了问题。在他的例子中,最后一行是一个等式,因此,我认为他对它没有问题,这并不重要……问题是:
是否有可能在函数和(…)
中获得这些值-因此这也是关于如何在函数中传递和使用
$sum1
$sum2
的问题,你是php之王,我没有时间讲这些。@Roki对不起,我不是有意冒犯你的。你们为什么不纠正一下你们的答案,这样我就可以对它进行投票,而访客们也不会因为看到不起作用的解决方案而感到困惑呢?交易?嘿,谢谢你的回答,但我想得到1的值。请查看我的完整信息code@hamp请更详细地描述它,最好使用示例。现在我不知道为什么这个解决方案不适合你,你需要什么变量。如果您说的是
$profit
变量,那么所提到的两个正确的解决方案(第一个和第二个)都应该适合您。请提供更多详细信息。您遇到了哪些错误?你能给我们更多关于代码结果的细节吗?首先,按照别人对
return
语句的说法去做。您的代码应该从函数返回
$total
,然后您应该告诉我们出了什么问题。