前言
在高階程式語言中,運算子是以符號表示的基本指令,無法再分割成更細微的指令。本文介紹 PHP 中常用的運算子。
算術運算子 (Arithmetic Operators)
算術運算子用來進行基本的代數運算。PHP 有以下算術運算子:
$a + $b:相加$a - $b:相減$a * $b:相乘$a / $b:相除,得浮點數intdiv($a, $b):相除,得整數。intdiv為函式而非運算子$a % $b:取餘數$a ** $b:取指數+$a:取其正數,即為本身-$a:取其負數
以下是簡短的範例:
<?php
$a = 10;
$b = 3;
$a + $b == 13 or die("$a + $b should be 13");
$a - $b == 7 or die("$a - $b should be 7");
$a * $b == 30 or die("$a * $b should be 30");
isEqualFloat($a / $b, 3.333333) or die("$a / $b should be close to 3.333333");
intdiv($a, $b) == 3 or die("(int) $a / $b should be 3");
$a % $b == 1 or die("$a % $b should be 1");
$a ** $b == 1000 or die("$a ** $b should be 1000");
# Comparison between floats.
function isEqualFloat($a, $b, $epsilon = 0.00001)
{
return abs($a - $b) < $epsilon;
}
指派運算子 (Assignment Operators)
指派運算子是一種語法糖。像是把 $a = $a + 3; 縮減為 $a += 3;。其他運算子也可以搭配指派運算子。
以下是簡短的範例程式:
<?php
$a = 5;
$a += 3;
8 == $a or die("Wrong number");
二元運算子 (Bitwise Operators)
二元運算子用來進行二元數運算。以下是 PHP 的二元運算子:
$a & $b:且 (bitwise and)$a | $b:或 (bitwise or)$a ^ $b:互斥或 (bitwise xor)~$a:取補數$a << $b:左移$b位數$a >> $b:右移$b位數
以下表格是 bitwise and 的運算方式:
| p | q | r |
|---|---|---|
| 1 | 1 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 0 | 0 | 0 |
記憶方式是「兩者皆為真才為真」。
以下表格是 bitwise or 的運算方式:
| p | q | r |
|---|---|---|
| 1 | 1 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 0 | 0 |
記憶方式為「兩者之一為真即為真」。
以下表格是 bitwise xor 的運算方式:
| p | q | r |
|---|---|---|
| 1 | 1 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 0 | 0 |
記憶方式是「兩者僅一為真才為真」。
以下範例程式進行數個二元運算。運算過程寫在註解裡:
<?php
$a = 3;
$b = 5;
/* 0011
&) 0101
--------
0001 */
($a & $b) == 1 or die("$a & $b should be 1");
/* 0011
|) 0101
--------
0111 */
($a | $b) == 7 or die("$a | $b should be 7");
/* 0011
^) 0101
--------
0110 */
($a ^ $b) == 6 or die("$a ^ $b should be 6");
/* 0011
<<) 1
---------
0110 */
($a << 1) == 6 or die("$a << 1 should be 6");
/* 0011
>>) 1
---------
0001 */
($a >> 1) == 1 or die("$a >> 1 should be 1");
二元運算一般用在低階運算,平日甚少用到。一開始不熟悉先略過無妨。
比較運算子 (Comparison Operators)
比較運算子用來比較兩值間的關係。以下是 PHP 的比較運算子:
==:兩者相等===:兩者相等且資料型態相等!=或<>:兩者相異!==:兩者相異或資料型態相異$a < $b:$a小於$b$a <= $b:$a小於或等於$b$a > $b:$a大於$b$a >= $b:$a大於或等於$b<=>;兩者互比,會得到整數值
以下範例程式進行一些比較運算:
<?php
$a = 7;
$b = 3;
!($a == $b) or die("$a and $b should not be equal");
$a != $b or die("$a and $b should be unequal");
$a > $b or die("$a should be larger than $b");
$a >= $b or die("$a should be larger than or equal to $b");
$b < $a or die("$b should be less than $a");
$b <= $a or die("$b should be less than or equal to $a");
<=> (spaceship) 回傳的數值不重要,重點是其回傳值相對於零的大小。以下是 <=> 的使用方式:
<?php
$a = 7;
$b = 3;
($a <=> $b < 0) or die("Wrong comparison");
邏輯運算子 (Logical Operators)
邏輯運算子用來進行邏輯運算。根據其優先順序,會再細分成兩組:
- 高優先次序
&&:且 (logical and)||:或 (logical or)!:否 (logical not)
- 低優先次序
and:且 (logical and)or:或 (logical not)xor:互斥或 (logical xor)
Logic and 的運算方式如下:
| p | q | r |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
Logic or 的運算方式如下:
| p | q | r |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Logic xor 的運算方式如下:
| p | q | r |
|---|---|---|
| true | true | false |
| true | false | true |
| false | true | true |
| false | false | false |
Logic not 的運算方式如下:
| p | r |
|---|---|
| true | false |
| false | true |
以下範例程式進行一些邏輯運算:
<?php
# and
(true and true) == true or die("true and true should be true");
(true and false) == false or die("true and false should be false");
(false and true) == false or die("false and true should be false");
(false and false) == false or die("false and false should eb false");
# or
(true or true) == true or die("true or true should be true");
(true or false) == true or die("true or false should be true");
(false or true) == true or die("false or true should be true");
(false or false) == false or die("false or false should be false");
# xor
(true xor true) == false or die("true xor true should be false");
(true xor false) == true or die("true xor false should be true");
(false xor true) == true or die("false xor true should be true");
(false xor false) == false or die("false xor false should be false");
# not
(!true) == false or die("not true should be false");
(!false) == true or die("not false should be true");
執行運算子 (Execution Operators)
執行運算子會執行命令列程式,將其輸出回傳到程式中。以下範例程式執行 ls(1):
<?php
$output = `ls -al`;
echo $output;
執行運算子等同於 shell_exec 函式。
運算子優先順序 (Operator Precedence)
同一條敘述內有多個運算子時,會按照運算子優先順序依序執行。這裡有一份 PHP 運算子優先順序的清單。
實際上,程式設計師 (不限於 PHP 程式設計師) 甚少記憶運算子優先順序。運算子優先順序符合數學運算方式。可以藉由撰寫簡明的敘述來減少單行敘述內的運算子數量。真的碰到較複雜的敘述時,可以用括號 ( 和 ) 改變運算子優先順序。