PHP section 2 data type-numeric _ php Basics-php Manual

Source: Internet
Author: User
Tags php basics type null
Two major functions of programming languages are data processing and process control. data types and interfaces are the basis for data processing. process control is a variety of control statements. program organization and coordination is a variety of programming paradigms; this section describes the basic data types of PHP.

PHP supports eight basic data types.

Four scalar types:

  • Boolean(Boolean)
  • Integer(Integer)
  • Float(Float type, also knownDouble)
  • String(String)

Two composite types:

  • Array(Array)
  • Object(Object)

There are two special types:

  • Resource(Resource)
  • NULL(NULL)

BOoleanData type:

The value can only be True or False. when other types are converted to the boolean type, the following values are consideredFALSE:

  • TheBooleanValueFALSEItself
  • TheInteger0 (0)
  • TheFloating point typeValue: 0.0 (0)
  • NullString, AndString"0"
  • Excluding any elementArray
  • Excluding any member variablesObject(Applicable only to PHP 4.0)
  • Special typeNULL(Including unconfigured variables)
  • Generated from an XML document without any tagsSimpleXMLObject

All other values are consideredTRUE(Including anyResources).

Integer data type:

The integer value can be expressed in decimal, hexadecimal, or octal format. you can add an optional symbol (-or +) to the front ).

The octal value indicates that the value must be added before the number.0(0), which must be added before a number in hexadecimal notation0x.

The length of the integer is related to the platform, although the maximum value is usually about 2 billion (32-bit signed ). PHP does not support unsigned integers.IntegerThe character length of a value can be a constant.PHP_INT_SIZEThe maximum value can be a constant after PHP 4.4.0 and PHP 5.0.5.PHP_INT_MAX.

If a given number exceedsIntegerWill be interpretedFloat. Similarly, if the execution result exceedsIntegerRange.Float.

PHP does not have the division operator.1/2GenerateFloat 0.5. You can always discard the decimal part, or useRound ()Function.

To explicitly convert a valueInteger, Use(Int)Or(Integer)Forced conversion. However, in most cases, no forced conversion is required, because when an operator, function, or flow control requiresIntegerThe value is automatically converted. You can also use functionsIntval ()To convert a value to an integer.

SlaveBoolean valueConversion, FALSEWill generate 0(Zero ), TRUEWill generate 1(1 ). SlaveFloating point numberTo convert a floating point to an integer. Toward ZeroInteger. If the floating point number exceeds the integer range (usually +/-2.15e + 9 = 2 ^ 31), The result is uncertain, because there is not enough precision to make the floating point number give an exact integer result. In this case, there is no warning or even no notification!

Float data type

The length of the floating point is related to the platform, although the maximum value is usually 1.8e308 with 14-bit decimal digits (64-bit IEEE format ).

Obviously, the simple decimal score is like0.1Or0.7It cannot be converted to an internal binary format without losing a little bit of precision. This results in confusion: for example,Floor (0.1 + 0.7) * 10)Usually7Not as expected8Because the internal representation of the result is similar.7.9.

This is related to the fact that it is impossible to accurately express certain decimal scores with limited digits. For example1/3Changed0.3.

Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal. If higher precision is required, use any precision mathematical function or gmp function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.