Get data type
1. If you want to check the value and type of an expression, use var_dump().
2. If you just want to get an easy-to-read type expression for debugging, use gettype().
3. To check a certain type, do not use gettype(), but use the is_type() function.
Convert string to numeric value
When a string is evaluated as a number, the following rules determine the type and value of the result.
If it contains any of the ".", "e" or "E" characters, the string is evaluated as a float. Otherwise it is treated as an integer.
The value is determined by the first part of the string. If the string begins with legal numeric data, that number is used as its value, otherwise its value is 0 (zero).
Legal numeric data begins with an optional sign, followed by one or more digits (optionally including a decimal fraction), followed by an optional exponent.
The exponent is an "e" or "E" followed by one or more digits.
Note: Don't expect to get the encoding of a character when you convert it to an integer (you probably do this in C as well). If you wish to convert between character encodings and characters, use the ord() and chr() functions.
forced type cast
Type casting in PHP is very much like in C: the variable to be converted is preceded by the target type enclosed in parentheses.
Allowed casts are:
(int),(integer) - 转换成整型
(bool),(boolean) - 转换成布尔型
(float),(double),(real) - 转换成浮点型
(string) - 转换成字符串
(array) - 转换成数组
(object) - 转换成对象Note that spaces and tabs are allowed within parentheses
You can also use settype (mixed var, string type) for forced conversion.
Cast to Boolean (bool)|(boolean)
To explicitly convert a value to boolean, use (bool) or (boolean) to cast. But in many cases, casting is not necessary because when an operator, function, or flow control requires a boolean parameter, the value is automatically converted.
When converted to boolean, the following values are considered FALSE:
Boolean value FALSE
Integer value 0 (zero)
Floating point value 0.0 (zero)
Blank string and string "0"
Array without member variables
Object without units (PHP 4 only)
Special type NULL (including variables not yet set)
All other values are considered TRUE (including any resources).
<?php
var_dump((bool) ""); // bool(false)
var_dump((bool) 1); // bool(true)
var_dump((bool) -2); // bool(true)
var_dump((bool) "foo"); // bool(true)
var_dump((bool) 2.3e5); // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array()); // bool(false)
var_dump((bool) "false"); // bool(true)
?>Cast to integer(int)|(integer)
To explicitly convert a value to an integer, use (int) or (integer) cast. In most cases, however, casting is not necessary because when an operator, function, or control flow requires an integer parameter, the value is automatically converted. You can also use the function intval() to convert a value to an integer type.
a.Convert from bool
b. Convert from floating point number to integer, out of range, result is uncertain
c. Convert from string See Convert String to Numeric Value
d. Convert from other types first to bool value and then convert
Never cast an unknown fraction to an integer, as this can sometimes lead to unexpected results.
<?php
echo (int) ( (0.1+0.7) * 10 ); // 显示 7
?>
$str = "123.456abc7"; // (int)123
echo (int)$str;
$str = "abc123.456"; // (int)0
$str = true; // (int)1
$str = false; // (int)0Cast to floating point type (int)|(double)|(real)|doubleval()|floatval()|intval()
Precision: 0.12345678901234 // Double and real are the same
Lost parameter of data Convert string to numeric value
Force conversion to string (string) |strval()
You can convert a value to a string using the (string) notation or the strval() function. When an expression requires a string, the conversion of the string is done automatically within the scope of the expression. For example when using the echo() or print() functions, or when comparing a variable value to a string.
布尔值 TRUE 将被转换为字符串 "1",而值 FALSE 将被表示为 ""(即空字符串)。这样就可以随意地在布尔值和字符串之间进行比较。
整数或浮点数数值在转换成字符串时,字符串由表示这些数值的数字字符组成(浮点数还包含有指数部分)。
数组将被转换成字符串 "Array",因此无法通过 echo() 或者 print() 函数来输出数组的内容。请参考下文以获取更多提示。
对象将被转换成字符串 "Object"。如果因为调试需要,需要将对象的成员变量打印出来,请阅读下文。如果希望得到该对象所依附的类的名称,请使用函数 get_class()。自 PHP 5 起,如果合适可以用 __toString() 方法。
资源类型总是以 "Resource id #1" 的格式被转换成字符串,其中 1 是 PHP 在运行时给资源指定的唯一标识。如果希望获取资源的类型,请使用函数 get_resource_type()。
NULL 将被转换成空字符串。
As shown above, printing an array, object, or resource does not provide any useful information about the values themselves.
See the functions print_r() and var_dump(), which are better ways of printing values for debugging.
PHP values can be converted to strings to store them permanently.
This method is called serialization and can be accomplished using the function serialize(). If you set up WDDX support when you installed PHP, you can also serialize PHP values into XML structures.
Cast to array (array)
对于任何的类型:整型、浮点、字符串、布尔和资源,如果将一个值转换为数组,将得到一个仅有一个元素的数组(其下标为 0),该元素即为此标量的值。
如果将一个对象转换成一个数组,所得到的数组的元素为该对象的属性(成员变量),其键名为成员变量名。
如果将一个 NULL 值转换成数组,将得到一个空数组。
Convert to object
If you convert an object to an object, it will not change anything. If a value of any other type is converted to an object, an instance of the built-in class stdClass will be created. If the value is NULL, the new instance is empty. Converting an array to an object will cause the keys to become property names with corresponding values. For any other value, a member variable named scalar will contain the value
Convert to resource (cannot convert)
Because resource type variables hold special handles for open files, database connections, graphics canvas areas, etc., values of other types cannot be converted to resources.