Categories
PHP

Find absolute, lowest, or highest value

How to find the absolute value of a number and find the minimum or maximum value from a set of mixed values.

abs() – Get absolute value of a number

<?php
 //Syntax
 abs(int|float $num): int|float

The absolute value of an integer or a float can be found with the abs( ) function. This function accepts one parameter $num (float or integer value) to process. If the $num is of type float, the return type is also float, otherwise, it is integer.

What is absolute value?

The absolute value is a non-negative value of a number, the absolute value of -10 is equal to 10, and the absolute value of -14.69 is equal to 14.69. See the following example:

<?php
 echo abs(-10);    # Prints: 10
 echo abs(10);     # Prints: 10
 echo abs(-14.69); # Prints: 14.69
 echo abs(14.69);  # Prints: 14.69

min() – Get lowest value

<?php
 //Syntax - 1
 min(mixed $value, mixed ...$values): mixed

 //Syntax - 2
 min(array $value_array): mixed

The min() function takes multiple parameters, if the first and only parameter is an array, it returns the lowest value in that array. If at least two parameters are provided, it returns the smallest of these values.

Example: Find the lowest value from two or more numbers

<?php
 $lowest = min(-1, -1.1, 8, 0, 3);
 echo $lowest; #Prints: -1.1

Example: Find the lowest value in an array

<?php
 $array = ['9', -1, -1.1, 8, 0, 3];
 $minVal = min($array);
 echo $minVal; # Prints: -1.1

max() – Get highest value

<?php
 //Syntax - 1
 max(mixed $value, mixed ...$values): mixed

 //Syntax - 2
 max(array $value_array): mixed

The max() function takes multiple parameters, if the first and only parameter is an array, it returns the highest value in that array. If at least two parameters are provided, it returns the highest of these values.

Example: Find the highest value from multiple numbers

<?php
 $highest = max(-1, -1.1, 8, 0, 3);
 echo $highest; #Prints: 8

Example: Find the highest value in an array

<?php
 $array = ['9', -1, -1.1, 8, 0, 3];
 $maxVal = max($array);
 echo $maxVal; # Prints: 9

Doing Math:

  1. Finding absolute value, lowest value, and highest value
  2. Using Ceil, Floor, and Round functions
  3. Generating random numbers
  4. Converting numbers between decimal, binary, octal, and hexadecimal
  5. Trigonometry, Exponential and Logarithmic Functions