# 類型概述
PHP支持十種原始數據類型,包括四種標量類型、四中復合類型以及兩種特殊的類型。PHP是一種弱類型的語言,所以不像其他語言一樣要顯示地聲明變量的數據類型,變量是什么數據類型是在運行時PHP自己根據變量的值決定的,所以了解數據類型只是增加理解。
想要知道一個表達式的類型和值,可以使用[var_dump() ](http://php.net/manual/en/function.var-dump.php)函數,想要知道一個變量的類型,可以使用[gettype()](http://php.net/manual/en/function.gettype.php)函數,如果想要確認一個變量是否是某個數據類型,請使用 *is_type* 函數,type用你想要確認的那個數據類型代替。
Example #1 gettype()函數和is_type()函數的
~~~
<?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";
}
?>
~~~
Example #2 var_dump()函數的使用
~~~
<?php
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
$b = 3.1;
$c = true;
var_dump($b, $c);
?>
~~~
自己在瀏覽器中查看運行結果吧,運行方法在快速入門中講過,以后不贅述。
當然我們有需要的時候,可以強制地將變量的數據類型進行[強制類型轉換](http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting),或者使用[settype()](http://php.net/manual/en/function.settype.php)函數進行設置。
Example #3 強制類型轉換
~~~
<?php
$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is a boolean
?>
~~~
Example #4 使用settype()函數設置變量的類型
~~~
<?php
$foo = "5bar"; // string
$bar = true; // boolean
settype($foo, "integer"); // $foo is now 5 (integer)
settype($bar, "string"); // $bar is now "1" (string)
?>
~~~