PHP
语言参考 | Language Reference

Introduction

Introduction

PHP supports ten primitive types.

Four scalar types:

  • boolean

  • integer

  • float (floating-point number, aka double)

  • string

Four compound types:

  • array

  • object

  • callable

  • iterable

And finally two special types:

  • resource

  • NULL

This manual also introduces some pseudo-types for readability reasons:

  • mixed

  • number

  • callback (aka callable)

  • array|object

  • void

And the pseudo-variable $....

Some references to the type "double" may remain in the manual. Consider double the same as float; the two names exist only for historic reasons.

The type of a variable is not usually set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used.

Note: To check the type and value of an expression, use the var_dump() function. To get a human-readable representation of a type for debugging, use the gettype() function. To check for a certain type, do not use gettype(), but rather the is_type functions. Some examples: <?php $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 $a\_bool is a string, print it out // (does not print out anything) if (is\_string($a\_bool)) {     echo "String: $a\_bool"; } ?>

To forcibly convert a variable to a certain type, either cast the variable or use the settype() function on it.

Note that a variable may be evaluated with different values in certain situations, depending on what type it is at the time. For more information, see the section on Type Juggling. The type comparison tables may also be useful, as they show examples of various type-related comparisons.

← Types

Booleans →

© 1997–2017 The PHP Documentation Group

Licensed under the Creative Commons Attribution License v3.0 or later.

https://secure.php.net/manual/en/language.types.intro.php