PHP Number Manipulations

A number is a mathematical object and one of the datatypes which is used to count, measure and calculation. In computer programming, a number is very important datatype. PHP has two types of numbers, Integers & Floating point numbers. PHP allows us to perform different PHP Number Manipulation operations on numbers for example comparing numbers, operation on series etc.

Check Valid Number

Using PHP's built in function is_int() and is_double(), it is possible to check whether variable is a number.

Check Valid Number Example

<?php 
$num = '130.12';
if(in_int($num) || in_double($num))
	echo '$num is a number';
?> 

This will produce following result

$num is a number

Check Number By Type

Using PHP's built in function gettype(), it is possible to check whether variable is a number by getting its type.

Check Number By Type Example

<?php 
$num = '130.12';
if(gettype($num) == 'integer' || gettype($num) == 'double')
	echo '$num is a number';
?> 

This will produce following result

$num is a number

Round off Floating Point Number

Using PHP's built in function round(), it is possible to round off floating point number through round() function.

Round off Floating Point Number Example

<?php 
$x = round(13.30);
echo $x;
?> 

This will produce following result

13

Compare Two Approximately Equal Floating Point Numbers

Using PHP's built in function abs(), it is possible to compare two approximately equal floating point numbers through abs() function.

Compare Two Approximately Equal Floating Point Numbers Example

<?php 
$x = 25.0000001;
$y = 25.0000000;
if(abs($x-$y)<0.00001){
echo "$x and $y are approximately equal.";
?> 

This will produce following result

25.0000001 and 25.0000000 are approximately equal.

Apply a Single Code to a Range of Integers

Using PHP's built in function abs(), it is possible to apply a single code to a range of integers using loops.

Apply a Single Code to a Range of Integers Example

<?php 
$x = 2;
$y = 8;
echo "List of even numbers between 2 and 8 is:"
for ($z = $x; $z <= $y; $z++){
echo $z/n;
?> 

This will produce following result

List of even numbers between 2 and 8 is:
2
4
6
8

Check Minimum Value in an Array

Using PHP's built in function sort(), it is possible to search for the number having minimum value in an array of unordered numbers.

Check Minimum Value in an Array Example

<?php 
$x = array(100,1,50,25,10);
$y = sort($x);
echo "Minimum value in the array is $y[0].";
?> 

This will produce following result

Minimum value in the array is 1.

Check Maximum Value in an Array

Using PHP's built in function sort(), it is possible to search for the number having maximum value in an array of unordered numbers.

Check Maximum Value in an Array Example

<?php 
$x = array(100,1,50,25,10);
$y = sort($x);
echo "Maximum value in the array is $y[sizeof($y)-1].";
?> 

This will produce following result

Maximum value in the array is 100.

Converting Between Binary and Decimal

Using PHP's built in function decbin() and bindec(), it is possible to convert a number into binary and decimal.

Check Maximum Value in an Array Example

<?php 
$num = 433;
$binary = decbin($num);
$decimal = bindec($binary);
?> 

Converting Between Octal and Hexadecimal

Using PHP's built in function octdec() and hexdec(), it is possible to convert a number into octal and hexadecimal.

Converting Between Octal and Hexadecimal Example

<?php 
$num = 433;
$binary = octdec($num);
$decimal = hexdec($binary);
?> 

Placing Commas in Numbers

Using PHP's built in function number_format(), it is possible to output a number with commas in the correct places.

Placing Commas in Numbers Example

<?php 
$num = 4330887.765;
echo number_format($num,2);
?>