note 58508 deleted from function.abs by derick

From: Date: Thu, 27 Aug 2009 08:02:07 +0000
Subject: note 58508 deleted from function.abs by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159918@lists.php.net to get a copy of this message
Note Submitter: concordia at game dot xakep dot org ---- 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.

« previous php.notes (#159918) next »