PHP Data Type
PHP supports eight types of raw data.
Four scalar types:
? Boolean (boolean)
? Integer)
? Float (float, also called double)
? String (string)
Two composite types:
? Array)
? Object)
There are two special types:
? Resource)
? NULL (no type)
To make sure the code is easy to understand, there are some pseudo types:
? Mixed (mixed type)
? Number)
? Callback (callback type)
Pseudo Variable $...
The type of a variable is generally not set by the programmer. To be exact, it is determined by PHP at runtime Based on the context used by the variable.
To view the value and type of an expression, use the var_dump () function. If you just want to get an easy-to-understand expression of the type for debugging, use the gettype () function. To view a type, use the is_type function instead of gettype. If you want to forcibly convert a variable to a certain type, you can use the force conversion or settype () function for it.
[Boolean]
To specify a Boolean value, use the keyword TRUE or FALSE. Both are case insensitive.
Convert to a Boolean Value
To convert a value to a boolean value explicitly, use (bool) or (boolean) to forcibly convert the value.
When converted to boolean, the following values are considered FALSE:
? Boolean value FALSE
? Integer value 0 (0)
? Floating point value: 0.0 (0)
? Empty string and string "0"
? Array that does not contain any elements
? Objects that do not include any member variables (applicable only to PHP 4.0)
? Special Type NULL (including unassigned variables)
? SimpleXML object generated from null tag
[Integer]
The integer value can be in decimal, hexadecimal, octal, or binary format. An optional Symbol (-or +) can be added before ).
Integer expressed in binary format is available in PHP 5.4.0. To use the octal expression, add 0 (zero) before the number ). To use a hexadecimal expression, you must add 0x before the number. To use a Binary Expression, you must add 0 B before the number.
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. The value can be forcibly converted to integer by dropping the decimal part, or the round () function can be used to better rounding.
Convert to integer
To explicitly convert a value to an integer, use (int) or (integer) to force the conversion.
[Float]
The precision of floating point is limited. Rational numbers that can be accurately expressed in decimal form, such as 0.1 or 0.7. No matter how many tails are there, they cannot be accurately expressed by the binary used internally, therefore, the binary format cannot be converted without a slight loss of 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. 9999999999999991118 ....
Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal. If higher precision is required, use any precision mathematical function or gmp function.
[NULL]
A special NULL value indicates that a variable has no value. The only possible value of the NULL type is NULL.
The variable is considered NULL in the following cases:
? The value is NULL.
? Not assigned.
? Unset ().
[Identification of type conversion]
The following mandatory conversions are allowed:
? (Int), (integer)-convert to integer
? (Bool), (boolean)-convert to boolean type boolean
? (Float), (double), (real)-convert to float
? (String)-convert to string
? (Array)-convert to array
? (Object)-convert to object
? (Unset)-convert to NULL (PHP 5)
[Variable handling function]
? Boolval-Get the boolean value of a variable
? Debug_zval_dump-Dumps a string representation of an internal zend value to output
? Alias of doubleval-floatval
? Empty-check whether a variable is empty
? Floatval-get the floating point value of the Variable
? Get_defined_vars-returns an array consisting of all Defined variables.
? Get_resource_type-returned resource Type
? Gettype-get the variable type
? Import_request_variables-import GET/POST/Cookie variables to the global scope
? Intval-obtains the integer of a variable.
? Is_array-check whether the variable is an array
? Is_bool-check whether the variable is Boolean
? Is_callable-check whether the parameter is a valid callable Structure
? Is_double-is_float alias
? Is_float-check whether the variable is floating point type
? Is_int-check whether the variable is an integer
? Is_integer-is_int alias
? Alias of is_long-is_int
? Is_null-check whether the variable is NULL
? Is_numeric-check whether the variable is a numeric or numeric string
? Is_object-check whether the variable is an object
? Alias of is_real-is_float
? Is_resource-check whether the variable is of the Resource Type
? Is_scalar-check whether the variable is a scalar
? Is_string-check whether the variable is a string
? Isset-check whether the variable is set
? Print_r-prints easy-to-understand information about variables.
? Serialize-generate a representation of a stored value
? Settype-set the variable type
? Strval-get the string value of the Variable
? Unserialize-create a PHP value from a stored Representation
? Unset-release given Variables
? Var_dump-prints information about a variable.
? Var_export-String Representation of the output or returned variable
';} Else {echo' variable $ bFlag is false '. '';} // integer $ iVal = 12345678; echo 'decimal result '. $ iVal. ''; $ a1 = 1234; // decimal number $ a2 =-123; // negative number $ a3 = 0123; // octal number (equal to decimal 83) $ a4 = 0x1A; // hexadecimal number (equal to decimal 26) $ large_number = 2147483647; echo var_dump ($ large_number ). ''; var_dump (25/7); // float (3.5714285714286) var_dump (int) (25/7); // int (3) // float $ fVal = 3.141592653; echo 'variable $ fVal value is '. $ fVal. ''; // null $ str1 = null; $ str2 = 'str '; If (is_null ($ st1) {echo '$ str1 is null '. '';} // debug a type of echo gettype ($ str2 ). ''; if (is_string ($ str2) {echo '$ str2 is of the string type '. '';} // destroy the object unset ($ str2); if (is_null ($ st1) {echo '$ str2 is null '. '';}?>