<?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";
}
preferences:
26.48 ms | 406 KiB | 5 Q