JustinK101 Posted March 1, 2010 Share Posted March 1, 2010 I need a function that does the following: input output -3 0 -2 0 -1 0 0 0 1 1 2 2 Basically returns only positive numbers, so not absolute value. If the number is negative, simply return 0. Is this a built in function into php? Thanks. Link to comment https://forums.phpfreaks.com/topic/193728-only-postive-numbers/ Share on other sites More sharing options...
trq Posted March 1, 2010 Share Posted March 1, 2010 Is this a built in function into php? Thanks. No, but it would be simple enough to write. Link to comment https://forums.phpfreaks.com/topic/193728-only-postive-numbers/#findComment-1019680 Share on other sites More sharing options...
bspace Posted March 1, 2010 Share Posted March 1, 2010 function mynumber($passed) { $num = ($passed + abs($passed)) / 2; return $num; } Link to comment https://forums.phpfreaks.com/topic/193728-only-postive-numbers/#findComment-1019696 Share on other sites More sharing options...
ignace Posted March 1, 2010 Share Posted March 1, 2010 function mynumber($passed) { $num = ($passed + abs($passed)) / 2; return $num; } No need for all this extra math keep it simple: function my_abs($value) { $value = intval($value); return $value < 0 ? 0 : $value; } Link to comment https://forums.phpfreaks.com/topic/193728-only-postive-numbers/#findComment-1019828 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.