note 56628 added to function.abs
| From: | dsi at danielsecuritiesinc dot com | Date: | Sat, 10 Sep 2005 01:43:08 +0000 |
| Subject: | note 56628 added to function.abs | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94974@lists.php.net to get a copy of this message | ||
Although it is not a requirement to do a check on the variable to be fed to abs(), it is suggested
merely out of good programming practices. A simple abs() statement could be:
<?php
if(is_numeric(abs($var))) return $var;
else return 0;
?>
For those of you who love short-cuts...
<?php
(is_numeric(abs($var)))?return $var:else return 0;
?>
Note that we don't check the variable to see if it is a number until <em>after</em>
we have computed the absolute value. It would be perfectly logical to do this instead:
<?php
if(is_numeric($var)) return abs($var);
else return 0;
?>
For those of you who love short-cuts...
<?php
(is_numeric($var))?return abs($var):else return 0;
?>
The thing to keep in mind that is in many languages, you might throw an error if $var is not a
number.
----
Manual Page -- http://www.php.net/manual/en/function.abs.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56628
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56628&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56628&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56628&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56628&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56628&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56628&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56628&report=yes
Search -- http://master.php.net/manage/user-notes.php