- 布林 (boolean)
- 整數 (integer)
- 浮點數 (floating-point number)
- 字串 (string)
第二大類為複合型態 (compound type)
- 陣列 (array)
- 物件 (object)
第三大類為特別的型態
- 資源 (resource)
- NULL
其中,布林、整數、浮點數、字串、 NULL 都有字面常數 (literal) 形式,使用時直接以字面常數設定給變數 (variable) 即可,陣列為配對型的資料結構,物件則為自訂類別 (class) 的型態,至於資源型態則是從內建函數 get_resource_type() 開啟的外部資源。
因此,變數可以是八種基本型態中的任一型態,可以隨意重新指派新值,舉例如下
<?php
$a = 22123;
echo "$a\n";
$a += 100;
echo "$a\n";
$a = "hello";
echo "$a\n";
$a = 0.321;
echo "$a";
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:vt01.php
功能:示範 PHP 程式
作者:張凱慶
時間:西元 2013 年 2 月 */
?>執行結果如下

也可以用指派為參考 (reference) ,就是一個別名 (alias) ,例如
<?php
$a = 11;
$b = &$a;
$c = $a;
echo "$a\n";
echo "$b\n";
echo "$c\n";
$a += 100;
echo "$a\n";
echo "$b\n";
echo "$c";
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:vt02.php
功能:示範 PHP 程式
作者:張凱慶
時間:西元 2013 年 2 月 */
?>這裡, $b 為 $a 的參考,定義參考需要用到 & 運算子標示在所要參考的變數前
$b = &$a;
因此當 $a 改變時, $b 也會隨之改變,反倒不是參考的 $c 仍是獨立的變數,此例執行結果如下

| 中英文術語對照 | |
|---|---|
| 純量型態 | scalar type |
| 布林 | boolean |
| 整數 | integer |
| 浮點數 | floating-point number |
| 字串 | string |
| 複合型態 | compound type |
| 陣列 | array |
| 物件 | object |
| 資源 | resource |
| 字面常數 | literal |
| 變數 | variable |
| 類別 | class |
| 參考 | reference |
| 別名 | alias |
您可以繼續參考
資料型態與參考
相關目錄
回 PHP 快速導覽
回 PHP 教材
回首頁
參考資料
http://www.php.net/manual/en/language.variables.basics.php
http://www.php.net/manual/en/language.types.php
http://www.php.net/manual/en/language.types.intro.php
http://www.php.net/manual/en/language.types.boolean.php
http://www.php.net/manual/en/language.types.integer.php
http://www.php.net/manual/en/language.types.float.php
http://www.php.net/manual/en/language.types.string.php
http://www.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/language.types.object.php
http://www.php.net/manual/en/language.types.resource.php
http://www.php.net/manual/en/language.types.null.php
沒有留言:
張貼留言