0

I have a repeating code in php that I would imagine I can somehow condense into a function and simply call the function multiple times. I tried doing this and nothing appears to happen. This is on repetition of the old code:

if ($health_1 > $health_2) {
    $health_left = '#7fba00';
    $health_right = '#e81123';
} else if ($health_perlvl_1 == $health_perlvl_2) {
    $health_left = '#0451ff';
    $health_right = '#0451ff';
} else {
    $health_left = '#e81123';
    $health_right = '#7fba00';
}

and this repeats about 12 times with other stats. I decided to try to condense it to this:

function stat_color($stat_1,$stat_2,$color_left,$color_right) {
    if ($stat_1 > $stat_2) {
        $color_left = '#7fba00';
        $color_right = '#e81123';
    } else if ($stat_1 == $stat_2) {
        $color_left = '#0451ff';
        $color_right = '#0451ff';
    } else {
        $color_left = '#e81123';
        $color_right = '#7fba00';
    }   

}
stat_color($health_1,$health_2,$health_left,$health_right);

But the colors are not there later when they are needed. Is there any way to actually get this to work?

4
  • 3
    Your functions are useless unless they actually DO something. Right now they're just setting local variables that are destroyed when the function returns. perhaps you want return(array('left' => '#7fba00', 'right' => '#e81123')); or similar. Commented Feb 28, 2014 at 20:32
  • @MarcB You could write the answer in the answer box too, just sayin Commented Feb 28, 2014 at 20:33
  • @MarcB I had return $color_left; and return $color_right; at the end and that did nothing. I'm not really sure how that array works Commented Feb 28, 2014 at 20:34
  • you can only return a single value from a function call. once you return, the function's dead and gone. hence the array - put whatever multiple values you want to return into that array and return the array. Commented Feb 28, 2014 at 20:54

3 Answers 3

2

Try this:

function stat_color($stat_1,$stat_2,&$color_left,&$color_right) {

That way it will update the variables you pass into the function.

Sign up to request clarification or add additional context in comments.

2 Comments

Understanding byref / byval relates to this question but I do not approve of this as a solution. I would rather see a function that 'returns' something.
+1 because it will work, though I agree with @ficuscr - pass-by-ref can be considered a side-effect.
1

Of course, since formal parameters are local to function scope their values cannot be used outside function. Try this.

Since we cannot return multiple values, I have used an array with appropriate keys.

function stat_color($stat_1,$stat_2) {

    $arr = array(); 
    if ($stat_1 > $stat_2) {
        $arr["color_left"] = '#7fba00';
        $arr["color_right"] = '#e81123';
    } else if ($stat_1 == $stat_2) {
        $arr["color_left"] = '#0451ff';
        $arr["color_right"] = '#0451ff';
    } else {
        $arr["color_left"] = '#e81123';
        $arr["color_right"] = '#7fba00';
    }   

    return arr;
}

You can now use:

$colors = stat_color(stat1,stat2);

Use $colors["color_left"] or $colors["color_right"] to refer to appropriate colors.

4 Comments

remove the color_ in $arr. They bloat the code. 'left' and 'right' and meaningful enough for a function named stat_color.
@udit: so you know, if you make an answer "community wiki", you don't get the reputation points. CW is useful if you are writing an answer on behalf of the OP, and wish to forego the rep points.
I did not know that. But I thought it might be helpful for me to make my answers wiki as other people can edit it if they feel something is wrong or can be improved, as I have just started to answer questions on this site. Thanks for information @halfer
People can edit your answers regardless of whether they are CW. If they have below a certain rep threshold (2,000 I think) their edits go into an approval queue; above this they are approved automatically.
0

You could return the strings thus:

return array('#7fba00', '#e81123');

And then when you want to get them again:

list($health_left, $health_right) = stat_color($health_1, $health_2);

You can do this with pass-by-reference variables, but it feels less elegant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.