WikiGalaxy

Personalize

PHP Numbers

Introduction to PHP Numbers

Understanding PHP Number Types:

PHP supports several data types for numbers, including integers, floats, and more. This section explores these types with examples.


<?php
$integer = 42;
$float = 3.14;
echo "Integer: $integer, Float: $float";
?>
    

Console Output:

Integer: 42, Float: 3.14

Integer Operations

Performing Basic Arithmetic:

PHP allows basic arithmetic operations on integers, such as addition, subtraction, multiplication, and division.


<?php
$a = 10;
$b = 5;
$sum = $a + $b;
$product = $a * $b;
echo "Sum: $sum, Product: $product";
?>
    

Console Output:

Sum: 15, Product: 50

Floating Point Precision

Handling Floating Point Numbers:

PHP handles floating-point numbers with double precision, allowing for operations with decimal points.


<?php
$x = 1.234;
$y = 2.345;
$sum = $x + $y;
echo "Sum of floats: $sum";
?>
    

Console Output:

Sum of floats: 3.579

Number Functions

Using Built-in Functions:

PHP provides several built-in functions to work with numbers, such as abs(), round(), and sqrt().


<?php
$number = -9.5;
echo "Absolute value: " . abs($number);
echo "Rounded value: " . round($number);
echo "Square root of 16: " . sqrt(16);
?>
    

Console Output:

Absolute value: 9.5, Rounded value: -10, Square root of 16: 4

Number Comparison

Comparing Numbers:

PHP allows for the comparison of numbers using operators like ==, !=, >, <, >=, and <=.


<?php
$a = 5;
$b = 10;
if ($a < $b) {
    echo "$a is less than $b";
}
?>
    

Console Output:

5 is less than 10

Type Casting in PHP

Casting Between Types:

PHP supports type casting between different number types, such as casting a float to an integer.


<?php
$float = 9.99;
$integer = (int)$float;
echo "Casted Integer: $integer";
?>
    

Console Output:

Casted Integer: 9

Scientific Notation

Using Scientific Notation:

PHP can handle scientific notation for numbers, which is useful for representing very large or very small values.


<?php
$largeNumber = 5E3;
echo "Large Number: $largeNumber";
?>
    

Console Output:

Large Number: 5000

Working with Infinity

Handling Infinite Values:

PHP can represent infinite values using the INF constant, which is useful in mathematical computations.


<?php
$infinity = INF;
echo "Infinity: $infinity";
?>
    

Console Output:

Infinity: INF

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025