Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Numbers

PHP has three primary numeric types:

  • Integer
  • Float
  • Numeric strings

Additionally, PHP includes two more numeric data types:

  • Infinity
  • NaN (Not a Number)

Numeric type variables are created by assigning a value to them:

Example

$a = 5;

$b = 5.34;

$c = “25”;

To check the type of any variable in PHP, use the var_dump() function:

PHP Floats

A float is a number that includes a decimal point or is expressed in exponential form.

Examples of floats include 2.0, 256.4, 10.358, 7.64E+5, and 5.56E-5.

The float data type can typically store values up to 1.7976931348623E+308 (platform dependent) and has a maximum precision of 14 digits.

PHP provides the following predefined constants for floats (available since PHP 7.2):

  • PHP_FLOAT_MAX: The largest representable floating-point number.
  • PHP_FLOAT_MIN: The smallest representable positive floating-point number.
  • PHP_FLOAT_DIG: The number of decimal digits that can be accurately rounded into a float and back without losing precision.
  • PHP_FLOAT_EPSILON: The smallest representable positive number xx such that x+1.0≠1.0x + 1.0 \neq 1.0.

PHP also includes the following functions to check if a variable is of float type:

  • is_float()
  • is_double() (alias of is_float())

Example

Determine if a variable is of float type:

$x = 10.365;
var_dump(is_float($x));

PHP Infinity

A numeric value greater than PHP_FLOAT_MAX is regarded as infinite.

PHP provides the following functions to check if a numeric value is finite or infinite:

  • is_finite()
  • is_infinite()

Additionally, the var_dump() function in PHP returns both the data type and the value.

Example

Determine if a numeric value is finite or infinite:

$x = 1.9e411;
var_dump($x);

PHP NaN

NaN stands for “Not a Number.”

NaN is used to represent the result of undefined mathematical operations.

PHP provides the following function to check if a value is NaN:

  • is_nan()

Additionally, the var_dump() function in PHP returns both the data type and the value.

Example

An invalid calculation will yield a NaN value.

$x = acos(8);
var_dump($x);

 

PHP Numerical Strings

The PHP is_numeric() function determines if a variable is numeric. It returns true if the variable is either a number or a numeric string; otherwise, it returns false.

 

Example

Verify if the variable is numeric:

$x = 5985;
var_dump(is_numeric($x));
$x = "5985";
var_dump(is_numeric($x));
$x = "59.85" + 100;
var_dump(is_numeric($x));
$x = "Hello";
var_dump(is_numeric($x));
Note: Starting from PHP 7.0, the is_numeric() function will return FALSE for numeric strings in hexadecimal format (e.g., 0xf4c3b00c), as they are no longer treated as numeric strings.

PHP Casting Strings and Floats to Integers

Sometimes, it’s necessary to cast a numerical value to a different data type.

The (int), (integer), and intval() functions are commonly used to convert a value to an integer.

Example

Cast a float or string to an integer:

// Cast float to int
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;
echo "<br>";
// Cast string to int
$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast;