Get DevWP - WordPress Development Theme

PHP Numbers

Subscribe To PixemWeb on YouTube

Working with numbers in PHP is not difficult once you understand how PHP handles various types of numbers.

Numbers are not all the same as you will see in this tutorial.

In this article I’ll show you how to work with Integers, Floats, and Number Strings.

PHP Integers

An Integer is a whole number. There are no decimal points.

Integers are between -2147483648 and 2147483647. A number that is larger or lower than that will be converted to a float.

As you can see, Integers can be either negative or positive.

You can use the PHP function is_int() to check if the number is an integer.

Below is some example code.


<?php
$i = 837;
var_dump(is_int($i));

$f = 100.67;
var_dump(is_int($f));
?>

The output would be:
bool(true)
bool(false)

Note: You can’t use a comma to separate numbers. This would cause an error. The way to format a large number would be to use the number_format() as demonstrated below.


<?php
$i = 6473829290;
echo number_format($i);
?>

The output would be 6,473,829,290

PHP Floats

A float is a number with a decimal point or a number in exponential form. A float can store a value up to 1.7976931348623E+308 and 14 digits is the maximum precision supported.

Just like PHP has the built in function is_int(), there’s also a built in function to check if a number is a float is_float().


<?php
$x = 5.65;
echo is_float($x);
?>

The output would be 1.
1 means true and if nothing is returned, then that means false.

PHP Numerical Strings

As mentioned in the tutorial on PHP Strings, numbers can also be in the string format. I’ll demonstrate this with some code below.


<?php
$test1 = '12345';
$test2 = 12345;

if ( is_int($test1) ) {
  echo '$test1 is an Integer <br>';
} elseif ( is_string($test1)) {
  echo '$test1 is a String <br>';
}

if ( is_int($test2) ) {
  echo '$test2 is a Integer';
}
?>

The output from the above test is:
$test1 is a String
$test2 is a Integer

Let’s break down the code above since I included some new code you might not have seen yet, or maybe you have.

  • We create two variables and asign them the same number but the first one is enclosed in quotes and the second one isn’t. When you have quotes around a number, it is considered a string.
  • We then have an if elseif which is a conditional which we’ll go over in another tutorial. But the basic explanation is it’s testing whether the variable provided is an integer using the is_int() function. If it’s true then we echo out the message $test1 is an Integer
  • If that condition fails, then we move onto the next part elseif which uses the PHP function is_string() to test if the variable is a string. If it is, then we output $test1 is a String. The BR tag is there just to force a line break.
  • The second test is for the $test2 variable and for this one, we just use the is_int() function.

As you can see, numbers in PHP can take various forms.



View Our Themes

Let's Connect