Php Getting Started Tutorial-detailed description of numeric data types-PHP Tutorial

Source: Internet
Author: User
Tags type null
Detailed description of the numeric data type in the php Getting Started Tutorial. PHP is a weak type. when using PHP, you do not need to define the type of a variable. you can use it to define the type of a variable. In PHP, the variable PHP is a weak type and does not need to define the type of the variable during use. you can use the edge to define the type of the number, the type of the variable you give is automatically 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. if the value is a string, it is a string.

The code is as follows:

$ Name = "your name"; // name string type.
$ Total = 12; // total is an integer.
?>


PHP supports eight primitive types ).

Four scalar types:

String (string)
Integer)
Float (float, also double)
Boolean (boolean)
Two composite types:

Array)
Object)
Two special types:

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

The code is as follows:

$ Var_bool = TRUE; // a boolean
$ Var_str = "foo"; // a string
$ Var_int = 12; // an integer

Echo gettype ($ var_bool); // output boolean
Echo gettype ($ var_str); // output string
Echo gettype ($ var_int); // output integer
?>

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 code is as follows:

$ Var_int = 12;

// If $ var_int is of the int type, add
If (is_int ($ var_int )){
$ Var_int = $ var_int + 4;
}
Echo $ var_int; // output 16
?>


Integer data type:

An integer is a set Z = {..., -2 ,...} A number in. 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.

For example:

The code is as follows:

$ A = 1234; // decimal number

$ A =-123; // a negative number

$ A = 0123; // The number of octal bytes (83 in decimal format)

$ A = 0x1A; // hexadecimal number (equal to 26 in decimal format)

?>

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 0 (zero) must be added before the number, and the hexadecimal value indicates that 0x must be added before the number.

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 is interpreted as float. Similarly, if the execution result exceeds the integer range, float is returned.

PHP does not have the division operator. 1/2 generates 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 the value is automatically converted when an integer parameter is required for the operator, function, or flow control. You can also convert a value to an integer using the intval () function.

To convert values from a Boolean value, FALSE will generate 0 (zero), and TRUE will generate 1 (one ).
To convert from a floating point number to an integer, it is rounded to zero. 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!

BooleanIs the simplest type. Boolean represents the TRUE value, which can be TRUE or FALSE. To specify a Boolean value, use the TRUE or FALSE keywords. Both values are case insensitive.

The code is as follows:

$ Foo = True; // assign the value true to $ foo.

?>

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

�� No unit object

�� 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 results in confusion: for example, floor (0.1 + 0.7) * 10) usually returns 7 instead of 8 in the expected result, because the internal representation of this result is similar to 7.9.

The code is as follows:

$ A = 76.60;
$ B = 76.00;

$ C = $ a-$ B;
Var_dump ($ c );

?>

The output is
Float (0.59999999999999)


However, this problem will not occur in 76.70.

The 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 value of decimal 1/3 is 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 code is as follows:

$ A = 1.234;

$ A = 1.2e3;

$ A = 7E-10;

?>

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

Bytes. In PHP, the variable...

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.