Both of the below codes were wrong.
<?php
// Check to see if $range is numberic, if not, set it to the integer value 1
if(!is_numeric($range)) {
$range=1;
}
// Check to see if $range is an integer and not a float. Use is_int() and not int().
if(!is_int($range)) {
// Make it an integer. Use intval() here, not int_val.
$range=intval($range);
}
$range=abs($range);
?>
The only thing I don't get is why we have to check before doing the functions. Whether or not we do the functions it will give us what we want. The only check I really understand being there is the one that sets it to one.
Why not just do:
<?php
// If $range is numberic, make it in positive integer, otherwise, make it one.
$range = is_numeric($range) ? abs(intval($range)) : 1;
?>
Seems like a lot less code. Or If you prefer to stay with if statements:
<?php
if(is_numberic($range)) {
// If $range is numberic, make it in positive integer.
$range = abs(intval($range));
}
else {
//otherwise, make it one.
$range = 1;
}
?>
Both ways seem smaller to me.