Detailed description of the numeric data type in the php Getting Started Tutorial

Source: Internet
Author: User
PHP is a weak type. when using PHP, you do not need to define the type of the variable. you can use the variable to define the type of the variable. in PHP, variable types are determined by the values assigned to them. if the value is an integer, the variable is an integer ,... PHP is a weak type. when using PHP, you do not need to define the type of the variable. you can use the variable to define the type of the variable.

In PHP, the type of variables is determined by the values assigned to them. if the value is an integer, the variable is an integer. if the value is a string, it is a string.

The instance code is as follows:

 

PHP supports eight primitive types ).

Four scalar types: string (string) integer (integer) float (float type, also double) boolean (boolean type) two composite types: array (array) object (object) two special types: resource (resource) NULL (NULL)

View variable types

The gettype () function allows you to conveniently view the type of a variable:

The instance code is as follows:

 

Prompt

For historical reasons, for float data, the gettype () function returns double instead of float.

To view the value and type of an expression, use the var_dump () function.

Judge variable type

To determine the next logical action by determining the variable type, use the is_type series functions instead of gettype:

The instance code is as follows:

 

Integer data type:

An integer is a set Z = {..., -2 ,...} The integer value can be specified in decimal, hexadecimal, or octal notation. an optional symbol (-or +) can be added before ).

If the octal symbol is used, 0 (zero) must be added before the number, and 0x must be added before the hexadecimal symbol.

The instance code is as follows:

 

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

"Octal" indicates that 0 (zero) must be added before the number, and "0x" must be added before the number in hexadecimal notation.

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. the length of the Integer value can be expressed by the constant PHP_INT_SIZE. after PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed by the constant PHP_INT_MAX.

If a given number exceeds the integer range, it will be interpreted as float. Similarly, if the execution result exceeds the integer range, float will be returned.

PHP does not use the divisible operator. 1/2 to generate float 0. 5. You can always discard the fractional part or use the round () function.

To explicitly convert a value to an integer, use (int) or (integer) to force the conversion. however, in most cases, no forced conversion is required, because when an integer parameter is required for an operator, function, or flow control, the value is automatically converted. you can also use the intval () function to convert a value to an integer.

Convert from a Boolean value. if the value is FALSE, 0 (zero) is generated. if the value is TRUE, 1 (one) is generated ).

To convert from a floating point number to an integer, it is rounded to zero. if the floating point number is out of 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!

Boolean is the simplest type. boolean represents the TRUE value, which can be TRUE or FALSE. to specify a boolean value, use the keyword TRUE or FALSE. Both are case insensitive.

The instance code is as follows:

 

The following values are considered FALSE:

Boolean value FALSE

Integer value 0 (0)

Floating point value: 0.0 (0)

Blank string and string "0"

Array without member variables

Objects without cells

Special type NULL (including unset variables) all other values are considered to be TRUE (including any resources)

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, a simple decimal score is like 0.1 or 0.7. it cannot be converted to an internal binary format without losing a little precision. this will lead to chaotic results: for example, floor (0.1 + 0.7) * 10) usually returns 7 instead of 8 in expectation, because the internal representation of the result is similar to 7.9.

The instance code is as follows:

 

The output is

float(0.59999999999999)

However, this problem will not occur in 76.70.

The instance code is as follows:

$a = 76.60;   $d = intval($a*100);   var_dump($d);   $a = 76.60; $d = intval($a*100); var_dump($d);

The output is int (7659)

That is indeed a 76.60 problem... (this problem is also reproduced in Java, Obj-C)

This is related to the fact that it is impossible to accurately express certain decimal scores with limited digits. for example, the decimal 1/3 is changed to 0.3.

Floating point type

Floating point numbers (also known as "floats", "doubles" or "real numbers") can be defined using any of the following syntax:

The instance code is as follows:

 

In php, we have explained these numeric types in detail. For more information, see.

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.