|
Posted by Steve on 02/12/07 21:10
| function checkInteger(&$value, $checks) {
| $err = '';
| if (!is_numeric($value) || (floatval($value) != intval($value))) {
| $err .= 'Input must be an integer. ';
| } else {
| $value = intval($value);
| if ($checks['MinValue'] != '' && $value <
| intval($checks['MinValue'])) {
| $err .= 'Minimum value = ' . $checks['MinValue'] . '. ';
| }
| if ($checks['MaxValue'] != '' && $value >
| intval($checks['MaxValue'])) {
| $err .= 'Maximum value = ' . $checks['MaxValue'] . '. ';
| }
| }
| }
function checkInt($value, $min, $max)
{
if (!is_numeric($value)){ return false; }
if (floatval($value) != intval($value)){ return false; }
}
don't use magic variables like $checks. either make legit params like
$min/$max or make $checks an object. as it is, you don't even check to see
if $checks is an array. further, don't live in nests. if there are
conditions that must be met before your code should run, then begin by
dropping out of the function *at the first possible opportunity* ... that
keeps the code not only neater but definitively shows what the conditions
are.
as for why your code didn't work before...well, you have to show us what the
code *was* before. most likely, you didn't cast correctly.
[Back to original message]
|