Convert negative numbers to positive one in PHP
When I am developing projects I came across a situation that I need to convert negative number to positive number. PHP comes with built-in function to handle it is abs(). We can pass integer and float as input to this and respective will be returned as positive value.
/** * CodeZone.in * Author: Dineshkumar * Contact : email@example.com */ <?php echo abs(-24); // 24 (integer) echo abs(-24.89); // 24.89 (float) echo abs(24); // 24 (integer) ?>
For an instance let us consider there is an order of things from 1 to 10 in a store and we want to know how many things a person crossed in this situation a person may start from 1 and go till 6. So total product crossed is 6. If in case he starts from 2 and ends in 6 then it is 5(6-2+1) what we will do if a person starts from 8 and ends in 1 you can easily say it is 8 but in programming logic it can be calculated by difference of 8 and 1 +1 here we need to convert negative value to positive value through abs() so it is abs(1-8)+1=8.