我怎样才能从函数内部获得价值?

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.

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

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

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