php 类型

来源:互联网 发布:淘宝套现可靠的店家 编辑:程序博客网 时间:2024/05/22 15:43

/*布尔型的真假
1 the boolean FALSE itself
2 the integer 0 (zero)
3 the float 0.0 (zero)
4 the empty string, and the string “0”
5 an array with zero elements
6 an object with zero member variables (PHP 4 only)
7 the special type NULL (including unset variables 未被赋值的变量)
8 SimpleXML objects created from empty tags*

summary(总结) -1 is considered TRUE, like any other non-zero (whether negative or positive 无论正负) number!
/

<?phpvar_dump((bool) "");        // bool(false)var_dump((bool) 1);         // bool(true)var_dump((bool) -2);        // bool(true)var_dump((bool) "foo");     // bool(true)var_dump((bool) 2.3e5);     // bool(true)var_dump((bool) array(12)); // bool(true)var_dump((bool) array());   // bool(false)var_dump((bool) "false");   // bool(true)?>

整形
To use octal notation, precede(之前) the number with a 0 (zero). To use hexadecimal notation precede the number with 0x. To use binary notation precede the number with 0b.

PHP does not support unsigned integers.

maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5

<?php$a = 1234; // decimal number$a = -123; // a negative number$a = 0123; // octal number (equivalent to 83 decimal)$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)$a = 0b11111111; // binary number (equivalent to 255 decimal)?>向八进制传入非法参数会自动截断<?phpvar_dump(01090); // 八进制 010 = 十进制 8?>

PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead

<?php$large_number = 9223372036854775807;var_dump($large_number);                     // int(9223372036854775807)$large_number = 9223372036854775808;var_dump($large_number);                     // float(9.2233720368548E+18)$million = 1000000;$large_number =  50000000000000 * $million;var_dump($large_number);                     // float(5.0E+19)?>

决不要将未知的分数强制转换为 integer,这样有时会导致不可预料的结果。

<?phpecho (int) ( (0.1+0.7) * 10 ); // 显示 7!?>

浮点数
The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision(精度) of roughly(大约) 14 decimal(十进制)(or 小数) digits is a common value (the 64 bit IEEE format).

Although it depends on the system, PHP typically(通常) uses the IEEE 754 double precision format(以前), which will give a maximum relative error(相对误差) (due to rounding)(取整) in the order of 1.11e-16. Non elementary(基本的) arithmetic(算数运算) operations may give larger errors, and, of course, error propagation (传递)must be considered when several operations are compounded(复合运算).

Additionally(此外), (rational(理性的) numbers)(有理数) that are exactly representation as floating point numbers in base 10(在10进制下准确表示的浮点数), like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally(内部的), no matter the size of the mantissa(尾数). Hence(因此), they cannot be converted into(转换) their internal binary counterparts(格式) without a small loss of precision(在不丢失精度的情况下). This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118….

testing floating point values for equality(相等) is problematic(问题的)

To test floating point values for equality, an upper bound on the relative error(误差) due to rounding is used(要使用一个仅比该数值大一丁点的最小误差值). This value is known as the machine epsilon(极小值), or unit roundoff(最小单元取整数), and is the smallest acceptable difference in calculations(计算中所能接受的最小的差别值.)

aandb are equal to 5 digits of precision (5位精确度).

<?php$a = 1.23456789;$b = 1.23456780;$epsilon = 0.00001;if(abs($a-$b) < $epsilon) {    echo "true";}?>

Some numeric operations can result in a value represented by the constant NAN. This result represents an undefined or unrepresentable value in floating-point calculations. Any loose or strict comparisons of this value against(对着) any other value, including itself, will have a result of FALSE.(任何拿此值与其它任何值进行的松散或严格比较的结果都是 FALSE)

0 0
原创粉丝点击