Home > Backend Development > PHP Tutorial > PHP Section 2 Numeric Data Type_PHP Tutorial

PHP Section 2 Numeric Data Type_PHP Tutorial

WBOY
Release: 2016-07-21 15:19:27
Original
1083 people have browsed it

PHP 支持8种基本的数据类型。

四种标量类型:

  • boolean (布尔型)
  • integer (整型)
  • float (浮点型, 也称作 double)
  • string (字符串)

两种复合类型:

  • array (数组)
  • object (对象)

最后是两种特殊类型:

  • resource (资源)
  • NULL (NULL)

boolean数据类型:

取值只能为True或者False,当其他类型转化为boolean类型时,以下值被认为是 FALSE

  • the 布尔FALSE 自身
  • the 整型值 0 (零)
  • the 浮点型值 0.0 (零)
  • 字符串, 以及字符串 "0"
  • 不包括任何元素的数组
  • 不包括任何成员变量的对象(仅PHP 4.0 适用)
  • 特殊类型 NULL (包括尚未设定的变量)
  • 从没有任何标记(tags)的XML文档生成的SimpleXML 对象

所有其它值都被认为是 TRUE(包括任何资源)。

integer数据类型:

整型值可以使用十进制,十六进制或八进制进行表示,前面可以加上可选的符号(- 或者 +)。

八进制表示数字前必须加上 0(零),十六进制表示数字前必须加上 0x

整型数的字长和平台有关,尽管通常最大值是大约二十亿(32 位有符号)。PHP 不支持无符号整数。Integer值的字长可以用常量PHP_INT_SIZE来表示,自 PHP 4.4.0 和 PHP 5.0.5后,最大值可以用常量PHP_INT_MAX来表示。

如果给定的一个数超出了 integer 的范围,将会被解释为 float。同样如果执行的运算结果超出了 integer 范围,也会返回 float

PHP 中没有整除的运算符。1/2 产生出 float 0.5。可以总是舍弃小数部分,或者使用 round() 函数。

要明确地将一个值转换为 integer,用 (int)(integer) 强制转换。不过大多数情况下都不需要强制转换,因为当运算符,函数或流程控制需要一个 integer 参数时,值会自动转换。还可以通过函数 intval() 来将一个值转换成整型。

Convert from boolean , FALSE will produce 0 (zero), TRUE will produce 1 (one) .

Convert from Float, when converting from float to integer, round towards zero. If the floating point number is outside the integer range (usually +/- 2.15e+9 = 2^31), the result is undefined because there is not enough precision for the floating point number to give an exact integer result. There is no warning in this case, not even any notification!

float data type

The word length of floating point numbers is platform-dependent, although typically the maximum value is 1.8e308 with a precision of 14 decimal digits (64-bit IEEE format).

Apparently simple decimal fractions like 0.1 or 0.7 cannot be converted to the internal binary format without losing a bit 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, because The internal representation of the result is actually similar to 7.9.

This is related to the fact that it is impossible to express certain decimal fractions accurately with a finite number of digits. For example, 1/3 in decimal becomes 0.3.

So never trust that a floating point number result is accurate to the last digit, and never compare two floating point numbers to see if they are equal. If you really need higher precision, you should use arbitrary precision math functions or the gmp function.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325306.htmlTechArticlePHP supports 8 basic data types. Four scalar types: boolean (boolean) integer (integer) float (floating point, also called double) string (string) Two composite classes...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template