<?php
function oldGetMinPrice($num1, $num2) {
if(!(is_numeric($num1) && $num1 >0) && !(is_numeric($num2) && $num2 >0) ) {
return 'error';
}
$num1 = (float)$num1;
$num2 = (float)$num2;
echo "($num1 & $num2) ";
if($num1 <= 0) {
return $num2;
}
if($num2 <= 0) {
return $num1;
}
return min($num1,$num2);
}
function newGetMinPrice($nums) {
$nums = array_filter($nums, function($v){ return (float)$v > 0;}); // convert to float and check if greater than 0
if (empty($nums)) {
return 'error'; // if no qualifying prices, return error
}
return min($nums); // return the lowest qualifying price
}
$tests = [[0, .1], [1, 'foo'], [.24, -.25], [3, 3], ['foo', 'bar'], [-0, 0.1], [90, -90], [0, 0], [1, 1]];
foreach ($tests as $test) {
echo "old: {$test[0]} -vs- {$test[1]} = ",oldGetMinPrice($test[0], $test[1]), "\n";
echo "new: {$test[0]} -vs- {$test[1]} = ",newGetMinPrice($test), "\n\n";
}
- Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.13
- old: 0 -vs- 0.1 = (0 & 0.1) 0.1
new: 0 -vs- 0.1 = 0.1
old: 1 -vs- foo = (1 & 0) 1
new: 1 -vs- foo = 1
old: 0.24 -vs- -0.25 = (0.24 & -0.25) 0.24
new: 0.24 -vs- -0.25 = 0.24
old: 3 -vs- 3 = (3 & 3) 3
new: 3 -vs- 3 = 3
old: foo -vs- bar = error
new: foo -vs- bar = error
old: 0 -vs- 0.1 = (0 & 0.1) 0.1
new: 0 -vs- 0.1 = 0.1
old: 90 -vs- -90 = (90 & -90) 90
new: 90 -vs- -90 = 90
old: 0 -vs- 0 = error
new: 0 -vs- 0 = error
old: 1 -vs- 1 = (1 & 1) 1
new: 1 -vs- 1 = 1
preferences:
133.22 ms | 409 KiB | 5 Q