Negative numbers to positive php

❮ PHP Math Reference

Example

Return the absolute value of different numbers:

echo(abs(6.7) . "
");
echo(abs(-6.7) . "
");
echo(abs(-3) . "
");
echo(abs(3));
?>

Try it Yourself »


Definition and Usage

The abs() function returns the absolute (positive) value of a number.


Syntax

Parameter Values

ParameterDescription
number Required. Specifies a number. If the number is of type float, the return type is also float, otherwise it is integer

Technical Details

Return Value:The absolute value of the number
Return Type:Float / Integer
PHP Version:4+

❮ PHP Math Reference


A trivial

$num = $num <= 0 ? $num : -$num ;

or, the better solution, IMHO:

$num = -1 * abs($num)

As @VegardLarsen has posted,

the explicit multiplication can be avoided for shortness but I prefer readability over shortness

I suggest to avoid if/else (or equivalent ternary operator) especially if you have to manipulate a number of items (in a loop or using a lambda function), as it will affect performance.

"If the float is a negative, make it a positive."

In order to change the sign of a number you can simply do:

$num = 0 - $num;

or, multiply it by -1, of course :)

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The abs() function is an inbuilt function in PHP which is used to return the absolute (positive) value of a number. The abs() function in PHP is identical to what we call modulus in mathematics. The modulus or absolute value of a negative number is positive.

    Syntax:

    number abs( value )

    Parameters: The abs() function accepts single parameter value which holds the number whose absolute value you want to find.

    Return Value: It returns the absolute value of the number passed to it as argument.

    Examples:

    Input : abs(6)
    Output : 6
    
    Input : abs(-6)
    Output : 6
    
    Input : abs(6.4)
    Output : 6.4
    
    Input : abs(-6.4)
    Output : 6.4
    

    Below programs illustrate the abs() function in PHP:

    Program 1: When a positive number is passed as a parameter:

    Output:

    6

    Program 2: When a negative number is passed as a parameter:

    Output:

    6

    Program 3: When a positive number with decimal places is passed as a parameter:

    Output:

    6.4

    Program 4: When a negative number with decimal places is passed as a parameter:

    php

    echo (abs(-6.4);

    ?>      

    Output:

    6.4

    Reference: http://php.net/manual/en/function.abs.php


    This is a short tutorial on how to convert negative numbers into positive numbers using PHP. In this guide, we will show you how to use PHP’s abs function.

    Take a look at the following snippet.

    //For the sake of this tutorial, let us say that our variable contains -6
    $ourNegativeNumber = -6;
    
    //To convert this minus number into a positive number, we simply use PHP's abs function.
    $positiveNumber = abs($ourNegativeNumber);
    
    //Print it out onto the screen.
    echo $positiveNumber;
    
    //The result should be 6.

    In the code above, we converted the number -6 to 6 using PHP’s abs function. The abs function will return the absolute number of a given variable.

    In case you didn’t already know, an absolute value is the non-negative value of a number.

    For example, the absolute “version” of -10 is 10.

    Note that the abs function also works with float values and decimal numbers.

    //A negative decimal number
    $ourNegativeDecimal = -6.24;
    
    //The abs function will also convert negative decimal numbers into positive decimals.
    $positiveDecimal = abs($ourNegativeDecimal);
    
    //Print out our float value onto the screen.
    echo $positiveDecimal; //becomes 6.24

    The return type from the abs function depends on the variable that you provide it with. For example, if you provide it with a float value, then the return type will also be a float. Otherwise, it will return an integer value.

    How do you turn a negative into a positive?

    All you have to do just multiply a negative value with -1 and it will return the positive number instead of the negative.

    How check if value is minus in PHP?

    In PHP we can find the absolute value of an integer by using the abs() function. For example if I were trying to work out the difference between two figures I could do this: $turnover = 10000; $overheads = 12500; $difference = abs($turnover-$overheads); echo "The Difference is ".

    What is use of abs PHP?

    The abs() function is an inbuilt function in PHP which is used to return the absolute (positive) value of a number. The abs() function in PHP is identical to what we call modulus in mathematics. The modulus or absolute value of a negative number is positive.

    How do you check if a number is positive negative or zero in PHP?

    PHP Source Code php $num = readline("Enter the number to be checked: \n"); if (! is_numeric($num)) echo "Enter a valid number"; elseif ($num > 0) echo "The entered number $num is positive"; elseif ($num < 0) echo "The entered number $num is negative"; else { echo "The entered number $num is zero"; } ?