2011-05-21 95 views
0

是否有可能得到这些值的函数中,并使用功能 这里外面那些是我的代码:如何从函数内部获取值?

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

现在我的问题是,我怎样才能得到totalincome和toatalexpenditure的价值? 我正在学习php新功能,所以请帮助我。

+6

我必须失去了一些东西,你为什么不只是返回从功能上totalincome和totalexpenditure? – sverre 2011-05-21 11:13:16

+0

你是否知道你的功能:1)你将有'$ profit'等于零,除非2)不将值赋给'$ sum1'和'$ sum2',而不会让它们成为'global'将会产生另一个问题? – Tadeck 2011-05-21 11:58:07

+0

请你可以选择第二个答案作为答案? – dynamic 2011-06-15 12:31:17

回答

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

+1

不,这是不正确的 - '$ sum1'和'$ sum2'没有被定义。 – Tadeck 2011-05-21 12:16:36

+0

但这不是问题,他在使用return语句方面存在问题。在他的例子中,最后一行是一个等式,所以我认为他没有任何问题,并且它并不重要...... – Roki 2011-05-21 12:24:10

+0

这个问题指出:“是否有可能在函数内部获得这些值? 。)' - 所以这也是如何在函数内部传递和使用'$ sum1'和'$ sum2'的问题。 – Tadeck 2011-05-21 12:30:54

2

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

  • 职权范围内的变量没有指定值(你应该通过函数参数中最好给它分配的,但另一个 - 工作,但错误 - 解决方案使它们成为全局变量),
  • 在给出的示例中,$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是一个坏主意(因为通常使用global变量将某些东西传递给函数是个坏主意)。

+0

嘿谢谢你的答案,但我想要得到一个值的1 veriables请参阅我的完整代码 – hamp 2011-05-21 12:39:56

+0

@hamp请描述它在很多更多的细节,最好使用示例。现在我不知道为什么解决方案不适合你,你需要什么变量。如果你在说'$ profit'变量,那么你提到的两个解决方案(第一和第二)都适用。请提供更多细节。 – Tadeck 2011-05-21 12:43:46

+0

嘿我编辑的问题,请告诉我的方式? – hamp 2011-05-21 13:11:29

0

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

$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