我如何从函数内部获取价值?

发布于 2024-11-09 00:50:56 字数 319 浏览 0 评论 0原文

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

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

现在我的问题是如何获得总收入和总支出的值? 我正在学习 php,所以请帮助我。

Is it possible to get those values inside of function and use those outside of function
here is my code:

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

now my question is how can i get value of totalincome and toatalexpenditure ?
i am learning php alos new in php so please help me guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你爱我像她 2024-11-16 00:50:56
<?
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) ;
?>

返回语句

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

return statement

二智少女猫性小仙女 2024-11-16 00:50:56

您的代码是错误的,因为:

  • 函数内的变量没有分配值(您最好通过函数参数分配它,但另一个 - 有效,但错误 - 解决方案是使它们成为全局变量),
  • 在给出的示例中, $profit 将始终为 0(零)。

解决方案共有三个:

解决方案编号。 1:

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

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

然后你可以像这样使用它:

$profit = income(10, 200) - expenditure(20,18);

解决方案编号。 2:

class Finances {
    public $expenditure = 0;
    public $income = 0;
    public function addExpense($expense) {
        $this->expenditure = $this->expenditure + $expense;
        return $this;
    }
    public function addIncome($income) {
        $this->income = $this->income + $income;
        return $this;
    }
    public function getProfit() {
        return $this->income - $this->expenditure;
    }
}

然后你就可以像这样使用它:

$my_finances = new Finances();
$my_finances->addExpense(20)->addExpense(18)->addIncome(10)->addIncome(10);
$profit = $my_finances->getProfit();

解决方案编号。 3:(避免使用!)

function expenditure() {
    global $sum1, $sum2;
    return $sum1 + $sum2;
}
function income() {
    global $sum1, $sum2;
    return $sum1 + $sum2;
}

然后你就这样使用它:

$sum1 = 10;
$sum2 = 200;
$expenditure = expenditure();
$sum1 = 20;
$sum2 = 30;
$income = income();
$profit = $income - $expenditure;

我希望你明白,为什么解决方案没有。 3 是一个坏主意(因为通常使用全局变量将某些东西传递给函数是坏主意)。

Your code is wrong, because:

  • the variables within functions do not have value assigned (you should assign it preferably by function parameters, but another - working, but wrong - solution is making them global variables),
  • in the example given, $profit will be always 0 (zero).

The solutions are three:

Solution no. 1:

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

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

And then you can use it like that:

$profit = income(10, 200) - expenditure(20,18);

Solution no. 2:

class Finances {
    public $expenditure = 0;
    public $income = 0;
    public function addExpense($expense) {
        $this->expenditure = $this->expenditure + $expense;
        return $this;
    }
    public function addIncome($income) {
        $this->income = $this->income + $income;
        return $this;
    }
    public function getProfit() {
        return $this->income - $this->expenditure;
    }
}

and then you can use it like that:

$my_finances = new Finances();
$my_finances->addExpense(20)->addExpense(18)->addIncome(10)->addIncome(10);
$profit = $my_finances->getProfit();

Solution no. 3: (avoid using!)

function expenditure() {
    global $sum1, $sum2;
    return $sum1 + $sum2;
}
function income() {
    global $sum1, $sum2;
    return $sum1 + $sum2;
}

And then you use it like that:

$sum1 = 10;
$sum2 = 200;
$expenditure = expenditure();
$sum1 = 20;
$sum2 = 30;
$income = income();
$profit = $income - $expenditure;

I hope you see, why the Solution no. 3 is such a bad idea (as generally using global variables to pass something to function is bad idea).

江南烟雨〆相思醉 2024-11-16 00:50:56

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

$var1 = 22;
$var2 = 15;

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

multi2($var1, $var2);
print $var1 . ", " . $var2;

您将得到此输出

44, 30

$x$y 参数本身不是变量,而是引用(由 & 定义)对于传递的变量,如果您需要在内部更改外部变量的值,这会很有帮助。

链接了解更多 http://php.net/manual/en/language.references .pass.php

This relates to another problem, that you may face at a later stage. What if you wanted to pass 2 variables in a function and change both their values.

$var1 = 22;
$var2 = 15;

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

multi2($var1, $var2);
print $var1 . ", " . $var2;

You will get this as an output

44, 30

The $x and $y parameters are not a variable themselves, but a reference (defined by &) to the variables passed through, this is helpful if you require to change the values external variables internally.

Link to understand more http://php.net/manual/en/language.references.pass.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文