PHP data type

Source: Internet
Author: User
Tags add empty expression functions variables string variable type null
PHP supports 8 of original data types.

Four scalar types:
? Boolean (Boolean)
? Integer (integral type)
? Float (floating point type, also called double)
? String (strings)

Two types of composite:
? Array (arrays)
? Object (Objects)

Finally, there are two special types of:
? Resource (resources)
? NULL (no type)

To ensure the readability of your code, there are also 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, specifically, by PHP based on the context used by the variable at runtime.
If you want to see the value and type of an expression, use the Var_dump () function. If you just want a readable type of expression for debugging, use the GetType () function. To see a type, do not use GetType () instead of the Is_type function. If you want to cast a variable to a type, you can use a cast or settype () function on it.

"Boolean"
To specify a Boolean value, use the keyword TRUE or FALSE. Two are not case-sensitive.
Convert to Boolean value
To explicitly convert a value to a Boolean, use (BOOL) or (Boolean) to cast.
When converted to Boolean, the following values are considered FALSE:
? Boolean value FALSE itself
? Integer value 0 (0)
? Floating-point value 0.0 (0)
? empty string, and string "0"
? An array that does not include any elements
? Objects that do not include any member variables (PHP 4.0 only applies)
? Special type NULL (includes 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 expression of integer is available from PHP 5.4.0. To use octal expression, you must add 0 (0) before the number. To use hexadecimal expressions, you must add 0x before the number. To use binary expressions, you must add 0b before the number.
The length of the Integer value can be represented by a constant php_int_size, which can be represented by a constant php_int_max since the PHP 4.4.0 and PHP 5.0.5.
If a given number exceeds the range of integer, it will be interpreted as float. Similarly, float is returned if the result of the execution of the operation exceeds the integer range.
The operator in PHP that is not divisible. 1/2 produces a float of 0.5. Values can be cast to integer by discarding the fractional part, or you can use the round () function for better rounding.
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, such as 0.1 or 0.7, that can be accurately expressed in decimal, no amount of mantissa can be accurately represented by the binary used internally, so it cannot be converted to a binary format without losing a little bit of precision. This can result in confusing results: for example, Floor (0.1+0.7) *10 usually returns 7 rather than the expected 8 because the internal representation of the result is actually similar to 7.9999999999999991118 ....
So never believe that floating-point numbers are accurate to the last one, and never compare two floating-point numbers for equality. If you do need higher precision, you should use arbitrary precision mathematical functions or GMP functions.

"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 null.
? has not been assigned a value.
? Be unset ().

"discriminant of type conversions"
The allowed casts are:
? (int), (integer)-Convert to Integer
? (bool), (Boolean)-Convert to Boolean type Boolean
? (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 internal Zend value to output
? Doubleval-floatval's Alias
? empty-Check if a variable is empty
? floatval-gets the floating-point value of a variable
? get_defined_vars-returns an array of all the defined variables
? get_resource_type-return Resource (Resource) type
? gettype-gets the type of the variable
? import_request_variables-Import the Get/post/cookie variable into the global scope
? intval-gets the integer value of a variable
? is_array-detect whether the variable is an array
? is_bool-detect whether the variable is Boolean
? is_callable-detection parameters are legitimate callable structures
? Is_double-is_float's Alias
? is_float-detect whether a variable is a floating-point type
? is_int-detect whether a variable is an integer
? Is_integer-is_int's Alias
? Is_long-is_int's Alias
? is_null-detect whether the variable is null
? is_numeric-detect whether a variable is a numeric or numeric string
? is_object-detect whether a variable is an object
? Is_real-is_float's Alias
? is_resource-detect whether a variable is a resource type
? is_scalar-detect whether a variable is a scalar
? is_string-detect whether a variable is a string
? isset-detect whether the variable is set
? print_r-Print Easy to understand information about variables.
? serialize-produces a representation of a value that can be stored
? settype-set the type of the variable
? strval-gets the string value of a variable
? unserialize-create PHP values from stored representations
? unset-releases the given variable
? var_dump-related information about printing variables

? var_export-output or return a string representation of a variable

!--? php//Boolean $bFlag = true;
	if ($bFlag) {echo & #39; variable $bflag is true & #39;. & #39; 
& #39;; else {echo & #39; variable $bflag is false & #39;. & #39; & #39;; }//integer $iVal = 12345678; Echo & #39; The result of the decimal and #39; $iVal. & #39; & #39; $a 1 = 1234; Decimal number $a 2 =-123; Negative $a 3 = 0123; Octal number (equal to decimal) $a 4 = 0x1A; Hexadecimal number (equal to decimal) $large _number = 2147483647; Echo var_dump ($large _number). & #39; & #39; Var_dump (25/7); Float (3.5714285714286) var_dump ((int) (25/7)); Int (3)//float $fVal = 3.141592653; Echo & #39; The value of variable $fval is & #39; $fVal. & #39; & #39; Null $STR 1 = NULL; $str 2 = & #39; str& #39; if (Is_null ($st 1)) {echo & #39; $str 1 is null& #39;. & #39; & #39;; //debug a type of Echo GetType ($str 2). & #39; #39 if (Is_string ($str 2)) {echo & #39; $str 2 is String type & #39;. & #39; }//Destroy Object Unset ($str 2); if (Is_null ($st 1)) {echo & #39; $str 2 is null& #39;. & #39; & #39;; }?>



-->

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.