PHP Introductory Tutorial Numerical data type detailed _php tutorial

Source: Internet
Author: User
Tags integer division type null
PHP is the weak type where you do not need to define a variable type when you use the edge to the number definition type, you give the variable what type of variable is automatically what type.

In PHP, the types of variables are determined by the values assigned to them. If the assigned value is an integer, then the variable is an integral type, and if it is a string, it is the string type.

The code is as follows Copy Code

$name = "Your name";//name bit string type.
$total = 12; Total is an integral type
?>


PHP supports eight primitive types (type).

Four types of scalar:

String (String)
Integer (integer type)
Float (floating point type, also double)
Boolean (Boolean)
Two kinds of composite types:

Array (arrays)
Object (Objects)
Two special types:

Resource (resources)
Null (empty)
View variable types
The GetType () function makes it easy to see the type of a variable:

The code is as follows Copy Code

$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
?>

Tips
For historical reasons, if the data is float type, the GetType () function returns a double instead of float.
If you want to see the value and type of an expression, use the Var_dump () function.
Judging variable types
If you want to determine the next logical action by judging the variable type, do not use GetType () and use the Is_type series function:

The code is as follows Copy Code

$var _int = 12;

If $var _int is of type int, this is an addition
if (Is_int ($var _int)) {
$var _int = $var _int+4;
}
echo $var _int; Output 16
?>


integer data type :

An integer is a number in the collection z={...,-2,-1,0,1,2,...}. Integer values can be specified in decimal, hexadecimal, or octal notation, preceded by an optional symbol (-or +).

If the octal notation is used, the number must be preceded by 0 (0) and the hexadecimal symbol must be preceded by a 0x.

For example:

The code is as follows Copy Code

$a = 1234; Decimal number

$a =-123; A negative number

$a = 0123; Octal number (equal to 83 in decimal)

$a =0x1a; Hexadecimal number (equals 26 in decimal)

?>

Integer values can be expressed in decimal, hexadecimal, or octal, preceded by an optional symbol (-or +).

Octal indicates that the number must be preceded by 0 (0), and hexadecimal indicates that the number must be preceded by 0x.

The word length of an integer is related to the platform, although the usual maximum value is approximately 2 billion (32-bit signed). PHP does not support unsigned integers. The length of the integer value can be expressed as a constant php_int_size, since PHP 4.4.0 and PHP 5.0.5, the maximum value can be expressed as a constant php_int_max.

If a given number exceeds the range of integers, it will be interpreted as float. Similarly, float is returned if the result of the operation is outside the integer range.

Operators that do not have an integer division in PHP. 1/2 produces a float of 0.5. You can always discard fractional parts, or use the round () function.

To explicitly convert a value to an integer, cast with (int) or (integer). However, in most cases there is no need to cast, because when an operator, function, or process control requires an integer parameter, the value is automatically converted. You can also convert a value to an integral type by using the function intval ().

From a Boolean value conversion, FALSE will yield 0 (0), and TRUE will produce 1 (one).
When converted from floating-point numbers to integers, 0 is rounded. If the floating-point number is outside the integer range (typically +/-2.15e+9 = 2^31), the result is indeterminate because there is not enough precision to give the floating-point number an exact integer result. In this case there is no warning, not even any notice!

The boolean type is the simplest. Boolean expresses the truth value, which can be TRUE or FALSE. To specify a Boolean value that uses the keyword TRUE or FALSE, two are case insensitive.

The code is as follows Copy Code

$foo =true; Assigns a value of 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"

An array with no member variables

An object without a cell

Special type NULL (including variables not yet set) all other values are considered TRUE (including any resources)

float data type

The length of the floating-point number is relative to the platform, although the usual maximum value is 1.8e308 and has a precision of 14-bit decimal digits (64-bit IEEE format).

It is obvious that a simple decimal score like 0.1 or 0.7 cannot be converted to an internal binary format without losing a little bit of precision. This can result in confusion: for example, floor ((0.1+0.7) *10 typically returns 7 instead of 8 as expected because the internal representation of the result is actually similar to 7.9.

The code is as follows Copy Code

$a = 76.60;
$b = 76.00;

$c = $a-$b;
Var_dump ($c);

?>

Output is
Float (0.59999999999999)


However, this problem does not occur with 76.70. Later found

The code is as follows Copy Code

$a = 76.60;
$d = intval ($a *100);
Var_dump ($d);
$a = 76.60;
$d = intval ($a *100);
Var_dump ($d);
Output is int (7659)

That's really 76.60 of the problem ... (Java,obj-c also reproduce the problem below)

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

Floating point Type

Floating-point numbers (also called "floats", "doubles" or "real numbers") can be defined in any of the following syntax:

The code is as follows Copy Code

$a = 1.234;

$a =1.2e3;

$a =7e-10;

?>

In PHP These types of numbers we have explained in detail the need to know the friends can refer to.

http://www.bkjia.com/PHPjc/629160.html www.bkjia.com true http://www.bkjia.com/PHPjc/629160.html techarticle PHP is the weak type where you do not need to define a variable type when you use the edge to the number definition type, you give the variable what type of variable is automatically what type. 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.