Numeric functions in php

is_numeric()

The is_numeric function used to check that the value of a variable is a number or not.

Syntax: is_numeric(var_name)

<?php
$var = 55;
if(is_numeric($var)){
	echo "Value is a number";
}
else{
	echo "Value is not a number";
}

?>

Output:

Value is a number

number_format()

The number_format function used to format a number with in a groups of thousands.

Syntax: implode(number,decimals,decimalpoint,separator)
(Number is required, other parameters are optional)

<?php
echo number_format("5000000")."<br>";
echo number_format("5000000",3)."<br>";
echo number_format("5000000",3,"/","*");
?>

Output:

5,000,000
5,000,000.000
5*000*000/000

range()

The range function used to create an array with containing the values with in the range.

Syntax: range(low,high,step)

<?php
$count = range(5,7);
print_r($count);
?>

Output:

Array (
[0] => 5
[1] => 6
[2] => 7
)

min()

The min function used to get the smallest value from an array or smallest value from some specified values.

Syntax: min(array_values); or min(value1,value2,…);

<?php
echo min(5,10,6,3,15);
?>

Output:

3

max()

The max function used to get the largest value from an array or largest value from some specified values.

Syntax: max(array_values); or min(value1,value2,…);

<?php
echo max(5,10,6,3,15);
?>

Output:

15

round()

The round function used to rounds a floating point number.

Syntax: round(number,precision,mode)

<?php
echo(round(0.85) . "<br>");
echo(round(0.50) . "<br>");
echo(round(0.39) . "<br>");
echo(round(-3.40) . "<br>");
echo(round(-3.60));
?>

Output:

1
1
0
-3
-4

floor()

The floor function used to rounds a number downnward to the nearest integer.

Syntax: floor(number)

<?php
echo(floor(0.55) . "<br>");
echo(floor(0.45) . "<br>");
echo(floor(5) . "<br>");
echo(floor(5.10) . "<br>");
echo(floor(-5.10) . "<br>");
echo(floor(-5.90));
?>

Output:

0
0
5
5
-6
-6

Leave a Reply