note 58508 added to function.abs

From: Date: Sun, 06 Nov 2005 21:35:21 +0000
Subject: note 58508 added to function.abs
Groups: php.notes 
Request: Send a blank email to php-notes+get-98168@lists.php.net to get a copy of this message
Sometimes you may want to do the opposite of abs(): turn a positive number into a negative. <?php function turn_neg ($num) { return $num - $num * 2; } ?> But this can create errors when you put a negative number inside... turn_neg (-2) returns 6. <?php turn_neg (-2); // 6. ?> The solution is to make another function to determine if the number is negative or not. <?php function is_neg ($num) { return $num < 0; } function turn_neg ($num) { if (is_neg ($num)) { return $num - $num * 2; } else { return abs ($num); } } turn_neg (2); // -2 turn_neg (-2); // 2 ?> Or, if the number is not negative, you could also return false. ---- Manual Page -- http://www.php.net/manual/en/function.abs.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58508 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58508&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58508&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58508&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58508&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58508&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58508&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58508&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58508&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98168) next »