# Thread: function to get the lowest value

1. ## function to get the lowest value

Hi, i am looking for i function that accepts one value at a time checks if the first value is less than 28, if true keeps it, gets the second value, checks it if it is the lowest so far and so on. the function will be called passing the value to be checked each time. Anybody seen any similar functions or tutorials around??
thanks, dh

2. How are you getting the values? Are they in an array or something?

3. Not sure if there's one in PHP (if there is I couldn't find it), so I rolled my own:
PHP Code:
``` function getLower( \$val1, \$val2 ) {     return (\$val1 < \$val2) ? \$val1 : \$val2; } // usage: \$num1 = \$_GET['a_number']; \$low_num = getLower( \$num1, 28 ); // etc.  ```
(edit: error in code; added sample usage)

4. Sorry should have included more details. I have so far used a function to work out 5 handicaps. What i need to do is find out which of these is the lowest and output that. I was thinking a function could hold the highest possible handicap(28) from the start and compare each score in turn to see if it was lower. I f it is then overwrite 28 with it and compare the next score to it and eventually have stored the lowest score.

print calculate (\$score1, \$par1, \$slope1);
print calculate (\$score2, \$par2, \$slope2);
print calculate (\$score3, \$par3, \$slope3);
print calculate (\$score4, \$par4, \$slope4);
print calculate (\$score5, \$par5, \$slope5);

function calculate(\$score, \$par, \$slope){
\$scoreDifference = \$score - \$par;
\$overallSlope = 113 / \$slope;
\$scoreDifferential = \$scoreDifference * \$overallSlope;
\$handicap = \$scoreDifferential * 0.96;
print getLowest(\$handicap;
return \$handicap;
}

5. I see what you are trying to do now. You may want to take advantage of the array sorting functions in php: (slight adjustment to code needed)
PHP Code:
``` \$handicap1 = calculate (\$score1, \$par1, \$slope1); \$handicap2 = calculate (\$score2, \$par2, \$slope2); \$handicap3 = calculate (\$score3, \$par3, \$slope3); \$handicap4 = calculate (\$score4, \$par4, \$slope4); \$handicap5 = calculate (\$score5, \$par5, \$slope5); # turn the above into an array for sorting \$handicaps = array( \$handicap1, \$handicap2, \$handicap3, \$handicap4, \$handicap5 ); sort( \$handicaps, SORT_NUMERIC ); # this is the value you wanted \$lowest_handicap = \$handicaps[0]; # removed print thing from function function calculate(\$score, \$par, \$slope) {   \$scoreDifference = \$score - \$par;   \$overallSlope = 113 / \$slope;   \$scoreDifferential = \$scoreDifference * \$overallSlope;   \$handicap = \$scoreDifferential * 0.96;   return \$handicap; }  ```

6. Would min work at all for you?

7. Originally Posted by vinyl-junkie
Would min work at all for you?
Absolutely - I've been re-inventing the wheel all along

Thanks Vinyl.

8. I'm glad I could help.

Here are a couple of other functions that might be useful for your application as well.

floor - to round fractions down

ceil - to round fractions up

Pretty cool, huh?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•