note 55694 added to function.abs
| From: | alreece45 at yahoo dot com | Date: | Wed, 10 Aug 2005 19:05:28 +0000 |
| Subject: | note 55694 added to function.abs | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-93391@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.abs.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55694
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55694&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55694&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55694&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55694&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55694&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55694&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55694&report=yes
Search -- http://master.php.net/manage/user-notes.php