PHP data type

Source: Internet
Author: User
Tags type null
: This article mainly introduces the PHP Data type. if you are interested in the PHP Tutorial, you can refer to it. 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 echo gettype ($ str2) of a type ).'
'; If (is_string ($ str2) {echo' $ str2 is of the string type '.'
';} // Destroy the object unset ($ str2); if (is_null ($ st1) {echo' $ str2 is Null '.'
';}?>


The above introduces the PHP Data type, including the content, and hope to help those who are interested in the PHP Tutorial.

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.