Sling Academy
Home/PHP/Explore built-in PHP functions to work with numbers

Explore built-in PHP functions to work with numbers

Last updated: January 09, 2024

Introduction

Whether it’s handling mathematical computations or formatting numbers for display, PHP offers a vast array of built-in functions to efficiently manage numeric values. This deep dive showcases how you can fully harness these functions for more effective number handling in your PHP applications.

Working with Basic Number Functions

PHP provides several functions to manipulate and analyze numbers. Let’s start by exploring some of the most commonly used numeric functions:

1. abs(): This function returns the absolute value of a number. Here’s a quick example:

$num = -15;
$absolute = abs($num);
echo $absolute;  // Outputs: 15

2. round(): It rounds numbers to the nearest integer or to a specified number of decimal places:

$num = 9.75;
$rounded = round($num);
echo $rounded;  // Outputs: 10

Advanced Mathematical Functions

PHP has functions for more complex mathematical tasks:

1. pow(): Raises a number to the power of another:

$base = 2;
$exp = 3;
$result = pow($base, $exp);
echo $result;  // Outputs: 8

2. sqrt(): Calculates the square root of a number:

$num = 16;
$squareRoot = sqrt($num);
echo $squareRoot;  // Outputs: 4

Formatting Numbers

Properly formatting numbers is crucial. To this end, PHP offers built-in functions, such as number_format().

number_format():

$num = 1000.75;
$formatted = number_format($num, 2, '.', ',');
echo $formatted;  // Outputs: 1,000.75

Operational Functions and Constants

PHP includes a suite of operational functions:

1. decbin(): Converts a decimal to a binary number:

$decimal = 2;
$binary = decbin($decimal);
echo $binary;  // Outputs: 10

2. bindec(): The reverse, converting binary to decimal:

$binary = '10';
$decimal = bindec($binary);
echo $decimal;  // Outputs: 2

Conclusion

PHP’s number functions provide developers with the toolkit required for handling numerical data with precision and ease. It’s crucial for any PHP developer to become familiar with these functions to enhance their coding capabilities and optimize application performance.

Next Article: PHP: Mapping numbers to words (e.g. 1 to ‘one’, 2 to ‘two’)

Previous Article: PHP: How to format a float to fixed width/length

Series: Working with Numbers and Strings in PHP

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array