1

I have set a variable in one function and I am trying to use it in another. People seem to be advising against using globals for functions.

This code is in my first variable

 if($this->CheckAndSetupContractHitman($player_data_tab,$player_defence_data_tab)) $winner_exp = $winner_exp + floor($winner_exp*0.4);

 if($this->CheckAndSetupContractHitman($player_data_tab,$player_defence_data_tab)) $bonus_exp = floor($winner_exp*0.2857); 

It is the variable $bonus_exp that I am trying to pass on to another function. Should I add return $bonus_exp; at the end of the first variable and then add $bonus_exp as an argument to the second variable like so?

function CheckAndSetupContractHitman($player_data_tab,$player_defence_data_tab,$bonus_exp)
  • I'm not sure I understand what you're asking properly, but as a general rule, if you have `function example1()` and you call it from `function example2()` anything in `example1()` you want to use in `example2()` pass it as an argument in `example2()`. Anything in `example2()` you want to use in `example1()`, return it from inside `example2()` There are exceptions of course, like passing by reference, but that's the general rule of thumb. –  Mar 19 '19 at 21:44
  • You also have globals, but, as you said, those aren't recommended. I'm not sure I ever knew the reasons for this, but I think it's to keep you from accidentally overwriting it somewhere. It's also better form to pass as a param and return. –  Mar 19 '19 at 21:46
  • 1
    @Chipster [because they are evil global variable](https://stackoverflow.com/a/19158418/2123530) but [not as evil as that in PHP](https://stackoverflow.com/a/1557799/2123530). *nota: two links in here* – β.εηοιτ.βε Mar 19 '19 at 22:02
  • First, understand the difference between "variable" and "function." And then, since you seem to be inside an object, you'll want to understand what a "property" and "method" are. The answer likely lies in using class properties. – miken32 Mar 20 '19 at 00:37

3 Answers3

1

Maybe you are looking for Passing by Reference

I think you should think out another solution for your problem, but Passing by Reference is better then using globals.

0

I would do as you suggest, using a return and passing the parameter. With one exception: Make it equal nothing (empty) -- That way .. The function doesn't technically need it as an input, and you can simply include it, or not, at your discretion.

function CheckAndSetupContractHitman($player_data_tab,$player_defence_data_tab,$bonus_exp = ''){
    if ($bonus_exp != ''){
        // Do something
    }
// ....
return bonus_exp;
}

Set up this way -- If you call the function without $bonus_exp IE

CheckAndSetupContractHitman($player_data_tab,$player_defence_data_tab);

$bonus_exp will be set to ''

But if you have it set and pass it in ... It will be set to whatever you passed. IE

$bonus_exp = 50;
CheckAndSetupContractHitman($player_data_tab,$player_defence_data_tab,$bonus_exp);

$bonus_exp would be passed as 50 into the function.

Zak
  • 5,117
  • 2
  • 19
  • 42
  • Okay thank you for your help. Am I right in thinking that return causes the function to end? So I will have to use return $bonus_exp; at the end of my first function to get it out of the function? – Dave Anthony Gordon Mar 19 '19 at 21:58
0

Basicly there are 2 ways to get some variables from a function.

Firstly as you said pass by reference.

Other one is a return value. It is possible to get more than 1 value with return, when you return an array like:

if(expression){
    return ["exp" => $exp, "bonus",$bonus];
} else {
   return false;
}    

So you can use like

if($array = topFunction($param1,$param2)){
    $exp = $array["exp"];
    $bonus = $array["bonus"];
}

I prefer second one. You should not execute 2 times same function.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Ahmet Uğur
  • 307
  • 5
  • 11