Skip to content

Numbers in php

Numbers in PHP

PHP supports various numeric data types and provides built-in functions for performing mathematical operations, conversions, and formatting.


1. Numeric Data Types in PHP

PHP has three main types of numbers:

  1. Integer (int) – Whole numbers (positive or negative).
  2. Floating-Point (float or double) – Decimal numbers.
  3. Number Strings – Strings containing numeric values (can be converted).

2. Integer (int)

An integer is a whole number without decimals, within the range:

  • 32-bit system: -2,147,483,648 to 2,147,483,647
  • 64-bit system: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Declaring an Integer

<?php

$num1 = 100;

$num2 = -50;

echo $num1; // Output: 100

?>

Checking if a Value is an Integer (is_int())

<?php

var_dump(is_int(100)); // Output: bool(true)

var_dump(is_int(10.5)); // Output: bool(false)

?>


3. Floating-Point (float or double)

A floating-point number is a number with a decimal point or in exponential form.

Declaring a Float

<?php

$price = 9.99;

$scientific = 5.1e3; // 5.1 × 10^3 = 5100

?>

Checking if a Value is a Float (is_float())

<?php

var_dump(is_float(10.5)); // Output: bool(true)

?>

Using Exponential Notation

<?php

$num = 2e3; // Equivalent to 2000

echo $num;  // Output: 2000

?>


4. Numeric Strings

A numeric string is a string that contains a valid number.

Checking if a String is Numeric (is_numeric())

<?php

var_dump(is_numeric(“123”)); // Output: bool(true)

var_dump(is_numeric(“123.45”)); // Output: bool(true)

var_dump(is_numeric(“Hello”)); // Output: bool(false)

?>

Converting a Numeric String to a Number

PHP automatically converts numeric strings during mathematical operations.

<?php

$x = “10”;

$y = $x + 5;

echo $y; // Output: 15

?>


5. Arithmetic Operations in PHP

PHP supports standard arithmetic operations:

OperatorDescriptionExample
+Addition$a + $b
Subtraction$a – $b
*Multiplication$a * $b
/Division$a / $b
%Modulus (Remainder)$a % $b
**Exponentiation$a ** $b

Examples of Arithmetic Operations

<?php

$a = 10;

$b = 3;

echo $a + $b; // Output: 13

echo $a – $b; // Output: 7

echo $a * $b; // Output: 30

echo $a / $b; // Output: 3.3333

echo $a % $b; // Output: 1

echo $a ** $b; // Output: 1000 (10^3)

?>


6. Rounding and Formatting Numbers

6.1 Rounding a Number (round())

<?php

echo round(3.6); // Output: 4

echo round(3.2); // Output: 3

?>

6.2 Rounding Up (ceil())

<?php

echo ceil(3.2); // Output: 4

?>

6.3 Rounding Down (floor())

<?php

echo floor(3.8); // Output: 3

?>

6.4 Formatting a Number (number_format())

<?php

$number = 1234.5678;

echo number_format($number, 2); // Output: 1,234.57

?>


7. Random Numbers

PHP provides functions to generate random numbers.

7.1 Generating a Random Number (rand())

<?php

echo rand(); // Outputs a random number

?>

7.2 Generating a Random Number within a Range

<?php

echo rand(1, 100); // Outputs a random number between 1 and 100

?>

7.3 Generating a Cryptographically Secure Random Number (random_int())

<?php

echo random_int(1, 100); // Outputs a secure random number between 1 and 100

?>


8. Mathematical Functions

PHP provides built-in math functions.

8.1 Absolute Value (abs())

<?php

echo abs(-10); // Output: 10

?>

8.2 Square Root (sqrt())

<?php

echo sqrt(16); // Output: 4

?>

8.3 Power (pow())

<?php

echo pow(2, 3); // Output: 8 (2^3)

?>

8.4 Logarithm (log())

<?php

echo log(10); // Natural logarithm (base e)

?>

8.5 Trigonometric Functions (sin(), cos(), tan())

<?php

echo sin(M_PI / 2); // Output: 1

?>


9. Type Conversion

PHP allows explicit conversion of numbers.

9.1 Casting to Integer ((int))

<?php

$num = 10.99;

$intNum = (int) $num;

echo $intNum; // Output: 10

?>

9.2 Casting to Float ((float))

<?php

$num = “10.5”;

$floatNum = (float) $num;

echo $floatNum; // Output: 10.5

?>

9.3 Converting a Number to a String (strval())

<?php

$num = 123;

$str = strval($num);

echo gettype($str); // Output: string

?>


10. Checking for Finite and Infinite Numbers

10.1 Checking for Infinity (is_infinite())

<?php

$num = 1.9e308;

var_dump(is_infinite($num)); // Output: bool(true)

?>

10.2 Checking for NaN (is_nan())

<?php

var_dump(is_nan(acos(2))); // Output: bool(true) (invalid mathematical operation)

?>


11. Conclusion

  • PHP supports integers, floats, and numeric strings.
  • PHP provides arithmetic operations like +, -, *, /, %, and **.
  • PHP has math functions for rounding (round(), ceil(), floor()), random number generation (rand(), random_int()), and advanced calculations (pow(), sqrt(), log()).
  • PHP can convert numbers to different types explicitly.

Understanding numbers in PHP is essential for performing calculations, handling user input, and processing financial data efficiently.