Hey,
I am currently using the following code for a matchmaker that I would like to use more effective:
$sports1 is the value I get from the database for player1. $sports2 is an array of values I get for player 2. If the values match, the score for player 2 is increased by 1:
if ($sports1 == '0' && $sports2 == '0') {$score[$player2]++;}
elseif ($sports1 == '1' && $sports2 == '1') {$score[$player2]++;}
elseif ($sports1 == '2' && $sports2 == '2') {$score[$player2]++;}
elseif ($sports1 == '3' && $sports2 == '3') {$score[$player2]++;}
else {};
I am using this code to compare different things, and the number of lines changes (depending on the number of possible numbers).
So, I thought that this should be much less code if I would be able to make a function out of this and I tried to make one, but of course, I failed.
Here is what I thought:
function increase_by_one($var_a, $var_b, $values)
{
if ($var_a == '0' && $var_b == '0') {$score[$player2]++;}
for($i=1; $i <= $values; $i++) {
elseif ($var_a == '$i' && $var_b == '$i') {$score[$player2]++;}
}
else {};
}
I call the function like this afterwards:
increase_by_one("$sports1", "$sports2", "3");
I hope ya'll won't kill me for psoting so much code of my first function that probably makes no sense. Could someone tell me how to do this so that it works? 🙂
Sarah