Home  >  Article  >  类库下载  >  PHP basic types

PHP basic types

高洛峰
高洛峰Original
2016-10-20 14:58:391489browse

PHP supports 8 primitive data types.


Four scalar types:

boolean (Boolean)

integer (integer)

float (floating point, also called double)

string (string)


two Specific composite type: aArray For readability, this manual also introduces some pseudo-types:

mixed (mixed type)

number (numeric type)

callback (callback type)

and pseudo-variables $….


You may also read some references to the "double" type. In fact, double and float are the same. For some historical reasons, these two names exist at the same time.

The type of a variable is usually not set by the programmer. Rather, it is determined by PHP at runtime based on the context in which the variable is used.

Note: If you want to check the value and type of an expression, use the var_dump() function. If you just want to get an easy-to-understand type expression for debugging, use the gettype() function. To check a type, don't use gettype(), use the is_type function. Here are some examples:

<?php
$a_bool = TRUE;   // a boolean
$a_str  = "foo";  // a string
$a_str2 = &#39;foo&#39;;  // a string
$an_int = 12;     // an integer
 
echo gettype($a_bool); // prints out:  boolean
echo gettype($a_str);  // prints out:  string
 
// If this is an integer, increment it by four
if (is_int($an_int)) {
    $an_int += 4;
}
 
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
    echo "String: $a_bool";
}
?>


If you want to force a variable to a certain type, you can use cast or settype() function.


Note that variables will exhibit different values ​​on specific occasions depending on their type at the time. See Type Conversion Discrimination for more information. In addition, you can also refer to the PHP type comparison table for examples of how different types compare to each other.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more