Home  >  Article  >  Backend Development  >  Summary of eight basic data types in PHP_PHP tutorial

Summary of eight basic data types in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:29:29740browse

Four scalar types:

  • boolean (Boolean type)
  • integer (integer type)
  • float (Floating point type, also called double)
  • string (string)

Two compound types:

  • array (array)
  • object (object)

Finally there are two special types:

  • resource (resource)
  • NULL (NULL)

To ensure code readability, this manual also introduces some pseudo-types:

  • mixed
  • number
  • callback

and pseudo variables $....

You may also read some references to the "double" type. In fact, double and float are the same, and for some historical reasons, these two names existed 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 var_dump().

If you just want to get a human-readable representation of the type for debugging, use gettype(). To check a certain type, do not use gettype(), but use the is_type function. Here are some examples:

Copy code The code is as follows:

$a_bool = TRUE; / / a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // 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 the settype() function.

Note that variables will exhibit different values ​​in certain situations depending on their type at the time. See Type Tricks for more information. Additionally, you can refer to the PHP Type Comparison Chart for examples of how different types compare to each other.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323433.htmlTechArticleFour scalar types: boolean (Boolean type) integer (integer type) float (floating point type, also known as double ) string (string) Two composite types: array (array) object (for...
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