
PHP 提供多樣、功能完整的運算子 (operator) ,依運算子結合後運算的優先次序 (precedence) ,如下列表
| clone new |
| [ |
| ++ -- ~ (int) (float) (string) (array) (object) (bool) @ |
| instanceof |
| ! |
| * / % |
| + - . |
| << >> |
| < <= > >= |
| == != === !== <> |
| & |
| ^ |
| | |
| && |
| || |
| ? : |
| = += -= *= /= .= %= &= |= ^= <<= >>= => |
| and |
| xor |
| or |
| , |
依功能區分,可分為
- 算術運算子 (arithmetic operator)
- 指派運算子 (assignment operator)
- 位元運算子 (bitwise operator)
- 比較運算子 (comparison operator)
- 錯誤控制運算子 (error control operator)
- 執行運算子 (execution operator)
- 遞增遞減運算子 (incrementing/decrementing operator)
- 邏輯運算子 (logical operator)
- 字串運算子 (string operator)
- 陣列運算子 (array operator)
- 型態運算子 (type operator)
算術運算子
算術運算子包含負數、加、減、乘、除、取餘數,至少需要一兩個運算元 (operand) 才構成運算式 (expression) ,如下列表
| 運算子 | 功能 | 範例 |
|---|---|---|
| - | 負數 | -$a |
| + | 加 | $a + $b |
| - | 減 | $a - $b |
| * | 乘 | $a * $b |
| / | 除 | $a / $b |
| % | 取餘數 | $a % $b |
指派運算子
最基本的指派運算子為單一個等號 = ,這是用來將等號右邊的值拷貝給給左邊的變數 (variable) 資料。等號也可以跟其他運算子合用,會直接將結果儲存到原變數之中,如
| 運算子 | 功能 | 範例 |
|---|---|---|
| = | 指派 | $a = 1 |
| += | 相加同時指派 | $a += 1 |
| .= | 連接同時指派 | $a .= 1 |
位元運算子
整數可進行逐位元運算的運算子,例如某 16 位元 (bit) 整數 (integer) 如下
0000111100001111
取其補數 (complement) 該整數會變成
1111000011110000
位元運算子如下列表
| 運算子 | 功能 | 範例 |
|---|---|---|
| & | 逐位元且 | $a & $b |
| | | 逐位元或 | $a | $b |
| ^ | 逐位元互斥或 | $a ^ $b |
| ~ | 逐位元非 | ~ $a |
| << | 向左位移 | $a << $b |
| >> | 向右位移 | $a >> $b |
比較運算子
比較運算子需要兩個運算元,包含小於、小於等於、大於、大於等於,另外有測試相等性的運算子,運算結果會是布林值,不是 True 就是 False
| 運算子 | 功能 | 範例 |
|---|---|---|
| == | 判斷兩個變數是否相等 | $a == $b |
| === | 判斷兩個變數是否相等而且型態相同 | $a === $b |
| != | 判斷兩個變數是否不相等 | $a != $b |
| <> | 判斷兩個變數是否不相等 | $a <> $b |
| !== | 判斷兩個變數是否不相等而且型態不相同 | $a !== $b |
| < | 判斷是否小於 | $a < $b |
| > | 判斷是否大於 | $a > $b |
| <= | 判斷是否小於或等於 | $a <= $b |
| >= | 判斷是否大於或等於 | $a >= $b |
錯誤控制運算子
錯誤控制運算子有 @ ,這是準備在發生錯誤時顯示錯誤訊息的。
執行運算子
執行運算子有 `` ,注意這跟單引號 ' 不同,這用來執行命令列指令。
遞增遞減運算子
遞增遞減運算子用來將變數遞增 1 或遞減 1
| 運算子 | 功能 | 範例 |
|---|---|---|
| ++ | 在運算式之前遞增 | ++$a |
| ++ | 在運算式之後遞增 | $a++ |
| -- | 在運算式之前遞減 | --$a |
| -- | 在運算式之後遞減 | $a-- |
邏輯運算子
邏輯運算子包含邏輯「非」,施用於單一運算元,其他邏輯「且」、邏輯「或」、邏輯「互斥或」都需要兩個運算元
| 運算子 | 功能 | 範例 |
|---|---|---|
| and | 且 | $a and $b |
| or | 或 | $a or $b |
| xor | 互斥或 | $a xor $b |
| ! | 非 | !$a |
| && | 且 | $a && $b |
| || | 或 | $a || $b |
字串運算子
字串運算子有 . ,這用來連接兩個字串。
陣列運算子
陣列運算子如下
| 運算子 | 功能 | 範例 |
|---|---|---|
| + | 取兩個陣列的聯集 | $a + $b |
| == | 判斷兩個陣列是否有相同的 key/value 配對 | $a == $b |
| === | 判斷兩個陣列是否有相同型態與順序的 key/value 配對 | $a === $b |
| != | 判斷兩個陣列是否不具有相同的 key/value 配對 | $a != $b |
| <> | 判斷兩個陣列是否不具有相同的 key/value 配對 | $a <> $b |
| !== | 判斷兩個陣列是否不具有相同型態與順序的 key/value 配對 | $a !== $b |
型態運算子
型態運算子有 instanceof ,用以判斷遍數是否屬於某一類別。
| 中英文術語對照 | |
|---|---|
| 運算子 | operator |
| 優先次序 | precedence |
| 算術運算子 | arithmetic operator |
| 指派運算子 | assignment operator |
| 位元運算子 | bitwise operator |
| 比較運算子 | comparison operator |
| 錯誤控制運算子 | error control operator |
| 執行運算子 | execution operator |
| 遞增遞減運算子 | incrementing/decrementing operator |
| 邏輯運算子 | logical operator |
| 字串運算子 | string operator |
| 陣列運算子 | array operator |
| 型態運算子 | type operator |
| 運算元 | operand |
| 運算式 | expression |
| 變數 | variable |
| 位元 | bit |
| 整數 | integer |
| 補數 | complement |
您可以繼續參考
基本概念
標記
相關目錄
回 PHP 快速導覽
回 PHP 教材
回首頁
參考資料
http://www.php.net/manual/en/language.operators.php
http://www.php.net/manual/en/language.operators.arithmetic.php
http://www.php.net/manual/en/language.operators.assignment.php
http://www.php.net/manual/en/language.operators.bitwise.php
http://www.php.net/manual/en/language.operators.comparison.php
http://www.php.net/manual/en/language.operators.errorcontrol.php
http://www.php.net/manual/en/language.operators.execution.php
http://www.php.net/manual/en/language.operators.increment.php
http://www.php.net/manual/en/language.operators.logical.php
http://www.php.net/manual/en/language.operators.string.php
http://www.php.net/manual/en/language.operators.array.php
http://www.php.net/manual/en/language.operators.type.php
沒有留言:
張貼留言