PHP data type

Source: Internet
Author: User
Tags integer division scalar type null
PHP supports 8 types of raw data.
four types of scalar:
? Boolean (Boolean)
? Integer (integer type)
? Float (floating point, also called double)
. String (String)
Two types of composite:
? Array (arrays)
? Object (Objects)
Finally, there are two special types:
? resource (Resources)
? NULL (no type)
to ensure the readability of the code, there are some pseudo-types:
? mixed (mixed type)
? number (numeric type)
? Callback (callback type)
pseudo-Variable $ ...
The type of the variable is usually not set by the programmer, but rather is determined by the context in which PHP is used according to the variable.
If you want to see the value and type of an expression, use theVar_dump() function. If you just want an easy-to-read type of expression for debugging, useGetType() function. To view a type, do not use GetType () and use the Is_type function. If you want to cast a variable to a type, you can use casts on it orSettype() function.
"Boolean"
to specify a Boolean value, use the keyword TRUE or FALSE. Two are not case-sensitive.
Convert to a Boolean value
to explicitly convert a value to a Boolean, use either (BOOL) or (Boolean) to cast.
When converted to Boolean, the following values are considered FALSE:
? The Boolean value FALSE itself
? Integer value 0 (0)
? floating-point value 0.0 (0)
An empty string, and the string "0"
? An array that does not include any elements
? An object that does not include any member variable (PHP 4.0 only applies)
? Special type NULL (including variables that have not been assigned)
SimpleXML object generated from an empty tag
"integer"
integer values can be expressed in decimal, hexadecimal, octal, or binary notation, preceded by an optional symbol (-or +).
the binary representation of the integer is available from PHP 5.4.0. To use octal expressions, the number must be preceded by 0(0). To use hexadecimal representation, the number must be preceded by 0x. To use binary representations, the numbers must be preceded by a 0b.
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 0.5. The value can be cast to integer by discarding the fractional part, or better rounded with the round () function.
convert to integral type
to explicitly convert a value to an integer, cast with (int) or (integer).
"Float"
the precision of floating-point numbers is limited. A rational number that can be accurately represented in decimal, such as 0.1 or 0.7, cannot be converted to a binary format without losing a little bit of precision, regardless of how many of the mantissa can be accurately represented by the binary used internally. 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.9999999999999991118 ....
so never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality. If you do need a higher precision, you should use any mathematical or GMP function of any precision.
"NULL"
A special NULL value indicates that a variable has no value. The only possible value for a null type is NULL.
A variable is considered NULL in the following cases:
? is assigned a value of NULL.
? has not been assigned a value.
? Be unset().
"Discrimination of type conversions"
the allowable casts are:
? (int), (integer)-Convert to Integer
? (bool), (Boolean)-converts to Boolean type bool
? (float), (double), (real)-Convert to float float
? (string)-Convert to String
? (array)-Convert array 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
? doubleval-floatval aliases
? empty-Check if a variable is empty
? floatval-gets the floating-point value of a variable
? get_defined_vars-returns an array of all defined variables
? get_resource_type-return Resource (Resource) type
? gettype-gets the type of the variable
? import_request_variables-importing Get/post/cookie variables into global scope
? intval-gets the integer value of the variable
? is_array-detects if a variable is an array
? is_bool-whether the detection variable is a Boolean type
? is_callable-detect if the parameter is a legitimate callable structure
? is_double-is_float aliases
? is_float-detect if the variable is floating-point
is_int-Detect if the variable is an integer
? is_integer-is_int aliases
? is_long-is_int aliases
is_null-Detect if the variable is null
? is_numeric-detects if a variable is a numeric or numeric string
? is_object-detects if a variable is an object
? is_real-is_float aliases
? is_resource-detects if a variable is a resource type
? is_scalar-whether the detection variable is a scalar
? is_string-detects if a variable is a string
? isset-detect if the variable is set
? print_r-prints Easy-to-understand information about variables.
? serialize-produces a representation of a value that can be stored
? settype-sets the type of the variable
? strval-gets the string value of the variable
? unserialize-to create PHP values from stored representations
? unset-releases the given variable
? var_dump-information about the print variable

? var_export-outputs or returns a string representation of a variable

 
  ';} Else{echo ' variable $bflag is false '. '
';} Integer$ival = 12345678;echo ' decimal result '. $iVal. '
'; $a 1 = 1234; Decimal number $a2 =-123; Negative $a3 = 0123; Octal number (equal to decimal) $a 4 = 0x1A; Hexadecimal number (equals decimal) $large _number = 2147483647;echo var_dump ($large _number). '
'; Var_dump (25/7); Float (3.5714285714286) var_dump ((int) (25/7)); The value of int (3)//Float$fval = 3.141592653;echo ' variable $fval is '. $fVal. '
';//null$str1 = null, $str 2 = ' str '; if (Is_null ($st 1)) {echo ' $STR 1 is null '. '
';} Debug a Type echo GetType ($str 2). '
'; if (is_string ($str 2)) {echo ' $str 2 is a string type '. ')
';} Destruction of Objects unset ($str 2); if (Is_null ($st 1)) {echo ' $str 2 is null '. '
';}? >


The above describes the PHP data types, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.