PHP supports ten primitive types.

Four scalar types:

Four compound types:

And finally two special types:

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

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:

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)) {
"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.

add a note add a note

User Contributed Notes 1 note

kuzawinski dot marcin_NOSPAM at gmail dot com
2 months ago
No, despite description here a `callable` still is not a a full-fledged primitive type in PHP.


function testFunc() { }

testClass {
    public function
__invole() { }
    public static function
testStaticMethod() { }
    public function
testMethod() { }

$o = new testClass();
$lambda = function() { };

$c1 = 'testFunc';
$c2 = ['testClass', 'testStaticMethod'];
$c3 = [$o, 'testMethod'];
$c4 = $lambda;
$c5 = $o;

var_dump(is_callable($c1));  // TRUE
var_dump(is_callable($c2));  // TRUE
var_dump(is_callable($c3));  // TRUE
var_dump(is_callable($c4));  // TRUE
var_dump(is_callable($c5));  // TRUE

var_dump(gettype($c1)); // string(6) "string"
var_dump(gettype($c2)); // string(5) "array"
var_dump(gettype($c3)); // string(5) "array"
var_dump(gettype($c4)); // string(6) "object"
var_dump(gettype($c5)); // string(6) "object"

To Top