note 56628 deleted from function.abs by derek
| From: | derek@php.net | Date: | Sat, 10 Sep 2005 02:02:19 +0000 |
| Subject: | note 56628 deleted from function.abs by derek | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94976@lists.php.net to get a copy of this message | ||
Note Submitter: dsi at danielsecuritiesinc dot com
Reason: bad code
----
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.