PHP supports 8 primitive data types. Four scalar types: boolean (Boolean), integer (integer), float (floating point type, also called double), string (string), two composite types: array (array), object (object), and finally There are two special types: resource (resource) and NULL (no type).
Note: If you want to check the value and type of an expression, use the var_dump() function. If you just want a human-readable representation of the type for debugging, use the gettype() function. To check a type, don't use gettype(), use the is_type function.
PHP is a weak language and will be automatically converted according to the program running environment. When using the == sign, if you compare a number and a string or compare a string involving numeric content, the string will be converted to Numerical values and comparisons are performed numerically. This rule also applies to switch statements. (Please use === for absolute comparison)
First operand type |
Second operand Type |
Type conversion |
Integer type |
Floating point type |
Convert integer type to floating point type |
##Integer type
|
String
|
Convert the string to a number. If the string is converted to a floating point type, the integer type will also be converted to a floating point type
|
Floating point type
|
String
|
Convert string to floating point type |
To summarize, floating point type> integer type> string> Boolean type
-
Boolean type
When converted to boolean, the following values are Considered FALSE:
All other values are considered TRUE (including any resources).
Boolean value FALSE itself
Integer value 0 (zero)
Floating point value 0.0 (zero)
The empty string, and the string "0"
An array containing no elements
Object that does not include any member variables (only applicable to PHP 4.0)
Special type NULL (including variables that have not been assigned a value)
SimpleXML object generated from empty tags
Integer type
Integer overflow: if A given number outside the range of integer will be interpreted as a float. Similarly, if the result of the operation exceeds the range of integer, float will be returned.
-
There is no integer division operator in PHP (unlike Java). 1/2 yields float0.5. The value can be cast to an integer, discarding the fractional part, or using the round() function for better rounding.
<?php
var_dump(25/7); // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7)); // float(4)
?>
When converting from a floating point number to an integer, it is rounded down.
-
Warning
Never cast an unknown fraction to an integer, as this can sometimes lead to unpredictable results.
<?php
echo (int) ( (0.1+0.7) * 10 ); // 显示 7!
?>
Float type
<?php
$a = 0.1;
$b = 0.9;
$c = 1;
var_dump(($a+$b)==$c);//true
var_dump(($c-$b)==$a);//falseprintf("%.20f", $a+$b); // 1.00000000000000000000
printf("%.20f", $c-$b); // 0.09999999999999997780?>
This problem occurs because floating point calculation involves precision. When floating point numbers are converted to binary May cause loss of accuracy.
So never believe that a floating point number is accurate to the last digit, and never compare two floating point numbers for equality.
If you really need higher precision, you should use arbitrary precision math functions.
高精度运算的方法如下:
bcadd 将两个高精度数字相加
bccomp 比较两个高精度数字,返回-1,0,1
bcp 将两个高精度数字相除
bcmod 求高精度数字余数
bcmul 将两个高精度数字相乘
bcpow 求高精度数字乘方
bcpowmod 求高精度数字乘方求模
bcscale 配置默认小数点位数,相当于Linux bc中的”scale=”
bcsqrt 求高精度数字平方根
bcsub 将两个高精度数字相减
As the above warning message states, due to internal expression reasons, there is a problem in comparing two floating point numbers for equality. However, there are roundabout ways to compare floating point values.
To test floating-point numbers for equality, use a minimum error value that is only a tiny bit larger than that value. This value, also known as the machine epsilon or smallest unit integer, is the smallest difference value that can be accepted in the calculation.
and are equal to five decimal places of precision.
<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
if(abs($a-$b) < $epsilon) {
echo "true";
}
?>
String type
If the string is enclosed in double quotes ("), PHP will parse some special characters: Such as \n, \\, \$
##The most important feature of strings defined with double quotes is that the variables will be parsed. Strings are spliced with '.' The type of values associated with keys. You can use the array() language structure to create a new array. It accepts any number of comma-separated key => value pairs
array( key => value , ...
)
// 键(key)可是是一个整数 integer 或字符串 string
// 值(value)可以是任意类型的值
此外 key 会有如下的强制转换:
<?php
$arr = array(5 => 1, 12 => 2);
$arr[] = 56; // This is the same as $arr[13] =56; at this point of the script
$arr["x"] = 42; // This adds a new element to the array with key "x"
unset($arr[5]); // This removes the element fromthe array
unset($arr); // This deletes the whole array
## Strings containing legal integer values will be converted to integers. For example, the key name "8" will actually be stored as 8, but "08" will not. It will be forced to convert because it is not a legal decimal value. The floating point number will also be converted to an integer, which means that the decimal part will be rounded off. For example, the actual key name 8.7. will be stored as 8.
##The Boolean value will also be converted into an integer. That is, the key name true will actually be stored as 1 and the key name false will be stored as 0.
# Null will be converted to an empty string, that is, the key name null will actually be stored as ""
Arrays and objects cannot be used. Key name. Insisting on this will result in the warning: Illegal offset type
If the same key name is used for multiple cells in the array definition, only the last one is used. are covered.
-
PHP arrays can contain both integer and string type key names, because PHP does not actually distinguish between index arrays and associative arrays
.
如果对给出的值没有指定键名,则取当前最大的整数索引值,而新的键名将是该值加一。
如果指定的键名已经有了值,则该值会被覆盖。
要删除某键值对,对其调用 unset() 函数。unset() 函数允许删除数组中的某个键。但要注意数组将不会重建索引。如果需要删除后重建索引,可以用 array_values() 函数。
foreach 控制结构是专门用于数组的。它提供了一个简单的方法来遍历数组。
数组(Array) 的赋值总是会涉及到值的拷贝。使用引用运算符通过引用来拷贝数组。
<?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,// $arr1 is still array(2, 3)
$arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>
NULL
特殊的 NULL 值表示一个变量没有值。NULL 类型唯一可能的值就是 NULL。
在下列情况下一个变量被认为是 NULL:
1. 被赋值为 NULL。2. 尚未被赋值。3. 被 unset()。
转换到 NULL :使用 (unset) $var 将一个变量转换为 null 将不会删除该变量或 unset 其值。仅是返回 NULL 值而已。
相关推荐:
PHP数据类型转换的转换
解析PHP数据类型之对象(Object)
PHP数据类型之字符串类型
PHP数据类型之布尔型变量详解
php数据类型
The above is the detailed content of Detailed explanation of php data type examples. For more information, please follow other related articles on the PHP Chinese website!