Difference between float and double in php?

Php

Php Problem Overview


I have this code

$vad = 1.1;

print gettype($vad);

var_dump($vad);

this will output:

double
float(1.1) 

So it is double or float in php?

Php Solutions


Solution 1 - Php

There is no difference in PHP. float, double or real are the same datatype.

At the C level, everything is stored as a double.
The real size is still platform-dependent.

See the manual for more details:
http://www.php.net/manual/en/language.types.float.php

Solution 2 - Php

For PHP, they are the same. http://www.php.net/manual/en/language.types.float.php :

> Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes: [...]

The confusing part is why gettype (which you shouldn't use, anyway) returns "double" instead of "float". The answer is http://de2.php.net/manual/en/function.gettype.php: > " double " (for historical reasons "double" is returned in case of a float , and not simply "float")

Solution 3 - Php

As of PHP 7.0.6 on Windows, comparing this command without xdebug:

$ php -r 'var_dump(28.4);'
float(28.4)

and with xdebug:

$ php -r 'var_dump(28.4);'
Command line code:1:
double(28.4)

Note that this only changes var_dump() output, but not the actual memory management.

This may address some concerns why you see double instead of float shown in var_dump in some other machines.

Also, with or without xdebug, gettype still returns string(6) "double".

Solution 4 - Php

In PHP 7.0.14

function test(double $a) {
    var_dump($a);
}
test(2.2111);

Returns "Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of double, float given".

function test(float $a) {
    var_dump($a);
}
test(2.2111);

Prints 2.2111 to the screen.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCenturionView Question on Stackoverflow
Solution 1 - PhpMacmadeView Answer on Stackoverflow
Solution 2 - PhpVictor NicolletView Answer on Stackoverflow
Solution 3 - PhpSOFeView Answer on Stackoverflow
Solution 4 - PhpOborotenView Answer on Stackoverflow