Is there a "double" type in PHP?

if not, how to declare a double type of number?

function testFloat(float $f) { return $f; } echo testFloat(1.2); 

Fatal error allowed: argument 1 passed to testFloat () must be an instance of float, double given

+7
php
source share
5 answers

Update:

Relatively enter hinting :

The type of hints can only be of the type of an object and an array (starting with PHP 5.1). The traditional hinting type with int and string is not supported.

Therefore, I do not know, but you will probably get an error, because only the types array and object supported.


I'm not quite sure what you want, but there is only a float :

Floating point numbers (also called "float", "doubleles" or "real numbers") can be specified using any of the following syntaxes:

 <?php $a = 1.234; $b = 1.2e3; $c = 7E-10; ?> 

and there you will also find:

Convert to float

For information on converting strings for swimming, see Converting Strings to Numbers . For values ​​of other types, conversion converts the value to an integer first and then floats. See Converting to an Integer for more information. Starting with PHP 5, a notification is called if the object is converted to a float.

+7
source share

The float type in PHP is implemented internally as a C double . This type of size is not specified by the C standard, which only requires sizeof(float) <= sizeof(double) <= sizeof(long double) .

Β§6.2.5.10

There are three real floating types: float , double and long double . 34) A set of values ​​of type float is a subset of a set of values ​​of type double ; a double value set is a subset of a long double value set.

(from here )

+4
source share
+2
source share

The error has nothing to do with float or double. Since the hinting type only works with an array or object, PHP considers the "float" to be a class. Do not use type hints for primitive scalar types. PHP is an untyped language. It doesn’t make sense.

For further use of my point, you can try this example,

 function testFloat(integer $f) { return $f; } echo testFloat(1); 

You get a similar error,

 Catchable fatal error: Argument 1 passed to testFloat() must be an instance of integer, integer given, called in /private/tmp/test.php on line 8 and defined in /private/tmp/test.php on line 3 
+2
source share

All Articles