Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

PHP Data Types:  Integers and Floating point numbers

PHP Integers

An integer must have at least one digit (0-9). No comma or blanks are allowed within an integer.

It must not have a decimal point.

It could be either positive or negative if no sign precedes it is assumed to be positive.

Integers can be specified in three formats, decimal (10-based), hexadecimal (16-based) (prefixed with 0x) or octal (8-based) (prefixed with 0) notation, optionally preceded by a - or + sign.

Example :

<?php
$x = 4356; //decimal number
echo $x.' is : ';
var_dump($x);
$x = -39; //a negative number
echo '<br />'.$x.' is : ';
var_dump($x);
$x = 057; //octal number (equivalent to 47 decimal)
echo '<br />'.$x.' is : ';
var_dump($x);
$x = 0x4B; //hexadecimal number (equivalent to 75
//decimal)
echo '<br />'.$x.' is : ';
var_dump($x);
?>

View the example in browser

Output:

4356 is : int(4356)
-39 is : int(-39)
47 is : int(47)
75 is : int(75)

Note: var_dump() function is used to get structured information (type and value) about variables.

PHP integers: Range

The size and range of an integer are platform depended. For a 32 bit computer maximum number the largest integer is 231-1 or 2,147,483,647 and the smallest integer is -(231-1) or -2,147,483,647. if we specify a number beyond the range of the integer type, it will be interpreted as a float. Integer size can be determined from PHP_INT_SIZE, maximum value from PHP_INT_MAX. If you specify a number beyond the bounds of the integer type, it will be interpreted as a float instead.

Floating point numbers

Description

Floating point numbers could be written in two forms, fractional and exponential form.

Fractional form :

The number must have at least one digit (0-9). No comma or blanks are allowed within an integer. It must have a decimal point. It could be either positive or negative, default sign is positive.

Exponential form :

The exponent part is an "e" or "E" followed by an integer, which can be signed (preceded by "+" or "-").

Version

(PHP 4 and above)

Example:

<?php
$a = 5.205;
$b = 1.3e3;
$c = 6E-10;
?>

Previous: Booleans
Next: Strings



PHP: Tips of the Day

How to Sort Multi-dimensional Array by Value?

Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

Example:

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
    return $a['order'] - $b['order'];
});

And finally with PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {
    return $a['order'] <=> $b['order'];
});

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {
    $retval = $a['order'] <=> $b['order'];
    if ($retval == 0) {
        $retval = $a['suborder'] <=> $b['suborder'];
        if ($retval == 0) {
            $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder'];
        }
    }
    return $retval;
});

If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual

Ref : https://bit.ly/3i77vCC