PHP Floating Points

Floating point numbersopen in new window or floats for short, are any number with a decimal in them. To set a variable to a floating point number, simply provide a number literal that contains a decimal.

<?php 
  // setting a variable to a float
  $float = 3.14;
?>

Mathematical Expressions

If a mathematical expression contains a float or a fraction or if the results of the expression is a number contains a decimal the result returned will be a floating point number.

<?php 
  // setting a variable to a float
  $float = 3.14;
  
  // adding 7 to a float
  echo $float + 7; // 10.14

  // this expression will result in a float
  echo 4 / 3; // 1.3333333333333
?>

NOTE

PHP does not let you divide by 0. This will result in an error.

More Mathematical Functions

PHP has many predefined Math Functionsopen in new window that are specifically for working with floats.

round() Function

The round()open in new window function will return the rounded value of the provided number by the specified precision.

<?php
  // setting a variable to a float
  $float = 3.14;
  
  // rounding $float to 1 decimal place
  echo round($float, 1); // 3.1
?>

ceil() Function

The ceil()open in new window function will return the next highest integer of the provided number.

<?php
  // setting a variable to a float
  $float = 3.14;
  
  // rounding $float to 1 decimal place
  echo ceil($float); // 4
?>

floor() Function

The floor()open in new window function will return the next lowest integer of the provided number.

<?php
  // setting a variable to a float
  $float = 3.14;
  
  // rounding $float to 1 decimal place
  echo floor($float); // 3
?>

Variable Handling Functions

Variable handling functionsopen in new window are used to receive information about a variable or to test the content contained in a variable.

is_int() Function

The is_int()open in new window function will find whether the data type of a variable is an integer. The function will return TRUE if the data type is an integer or FALSE if it is not.

NOTE

The var_dump()open in new window function output information about a variable, including value and type. Unlike echo, var_dump() is intended for debugging and development purposes only.

<?php
  // setting a variables
  $float = 3.14;
  $integer = 3;
  
  // checking if $integer is an integer
  var_dump(is_int($integer)); // bool(true)

  // check if $float is an integer
  var_dump(is_int($float)); // bool(false)
?>

is_float() Function

The is_float()open in new window function will find whether the data type of a variable is a floating point number. The function will return TRUE if the data type is a float or FALSE if it is not.

<?php
  // setting a variables
  $float = 3.14;
  $integer = 3;
  
  // checking if $integer is a float
  var_dump(is_float($integer)); // bool(false)

  // check if $float is a float
  var_dump(is_float($float)); // bool(true)
?>

is_numeric() Function

The is_numeric()open in new window function will find whether the data type of a variable is a number or a numeric string. The function will return TRUE if the data type is a number or a numeric string or FALSE if it is not.

<?php
  // setting a variables
  $float = 3.14;
  $integer = 3;
  
  // checking if $integer is a number
  var_dump(is_numeric($integer)); // bool(true)

  // check if $float is a number
  var_dump(is_numeric($float)); // bool(true)
?>