Alex Rivera | Logout

Default php function that turns negative numbers in 0

Asked 2011-07-03T23:25:48.483
62

Is there such a thing?

for eg

$var = -5;
echo thefunction($var); // should be 0


$var = 5;
echo thefunction($var); // should be 5
Edit
Report

2 Answers

198

Try max($var,0), which will have the desired effect. See the manual page for more information.

answered 2011-07-03T23:27:52.040
5

Not built-in but, here you have:

function thefunction($var){
   return ($var < 0 ? 0 : $var);
}

Hope this helps

answered 2011-07-03T23:28:55.880

Your Answer