Advertisement

Google Ad Slot: content-top

PHP Numbers

~1 min read · PHP
On this page

Numbers are a data type used for mathematical calculations and numerical data processing. PHP supports different types of numbers, such as integers, floating-point numbers (floats), and provides a wide range of functions to perform mathematical operations.


Types of Numbers in PHP


Type

Description

Example

Integer

Whole numbers (positive or negative) without decimals

10, -5, 1000


Try it yourself

Float (Double)

Numbers with decimals

10.5, -3.14, 2.718


Try it yourself

Infinity

Special value for numbers too large

NaN (Not a Number)

Used for invalid calculations

Numeric Strings

Strings containing numbers that can be used in calculations

"100", "3.14"


Try it yourself


PHP Casting Strings (Type casting)


PHP offers various ways to cast a numerical value into another data type.


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

Example
<?php
$str = "123.45";
$intValue = (int)$str; // Converts to integer (123)
$floatValue = (float)$str; // Converts to float (123.45)
echo $intValue."<br>";
echo $floatValue;
?>


Try it yourself