PHP | abs() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
echo (abs(6);
  
?>      

chevron_right


Output:

6

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
echo (abs(-6);
  
?>

chevron_right


Output:

6

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
echo (abs(6.4);
  
?>      

chevron_right


Output:

6.4

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
echo (abs(-6.4);
  
?>      

chevron_right


Output:

6.4

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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.