134

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?

4 Answers 4

180

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

6
  • sure about that? are there some memory use diffrent about it? Jan 1, 2017 at 12:11
  • 3
    I have function function some( float $num){}, when i use some(17.23) it throws fatal error some() must be an instance of float, double given,
    – Naveen DA
    Sep 2, 2017 at 9:02
  • 2
    @NaveenDA What version of PHP? Prior to PHP 7.0, scalar type hints were not available and would be interpreted as class names instead. That looks like what is going on there. Nov 1, 2017 at 18:50
  • just clarifying there's no such datatype real in php so far. May 22, 2019 at 15:21
  • @MuhammadSheraz But there's a is_real function, with is an alias of is_float... ; )
    – Macmade
    May 22, 2019 at 16:55
24

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")

11

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".

0
7

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.

2
  • 1
    Updated link php.net/language.types.declarations
    – Zombo
    Nov 16, 2020 at 19:40
  • As the "Type declarations" PHP manual link says, "Aliases for the above scalar types are not supported.". So it's probably best to stick with float, which is the official type name and is the most well-supported by the language. Mar 22, 2022 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.