开发者

is there a "double" type in PHP

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

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

echo test开发者_运维知识库Float(1.2);

Catchable fatal error: Argument 1 passed to testFloat() must be an instance of float, double given


Update:

Regarding type hinting:

Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn't supported.

So I don't know, but probably you get the error because only array and object types are supported.


I am not exactly sure what you want, but there is only float:

Floating point numbers (also known as "floats", "doubles", 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 find also:

Converting to float

For information on converting strings to float , see String conversion to numbers. For values of other types, the conversion is performed by converting the value to integer first and then to float . See Converting to integer for more information. As of PHP 5, a notice is thrown if an object is converted to float .


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

§6.2.5.10

There are three real floating types, designated as float, double, and long double.34) The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double.

(from here)


http://php.net/manual/en/language.types.float.php


The error has nothing to do with float or double. Since type hinting only works with array or object, PHP thinks "float" is a class. Don't use type-hinting for primitive scalar types. PHP is an untyped language. It doesn't make sense to do that.

To further drive my point, you can try this example,

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

echo testFloat(1);

You get the 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


http://php.net/manual/en/language.types.float.php
http://www.php.net/manual/en/function.settype.php
And, please, read See Also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜