Home > 类库下载 > PHP类库 > body text

PHP basic types

高洛峰
Release: 2016-10-20 14:58:39
Original
1534 people have browsed it

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";
}
?>
Copy after login


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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!