Forum und email

各類運算符

Table of Contents

An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value (so that the construction itself becomes an expression). So you can think of functions or constructions that return a value (like print) as operators and those that return nothing (like echo) as any other thing.

There are three types of operators. Firstly there is the unary operator which operates on only one value, for example ! (the negation operator) or ++ (the increment operator). The second group are termed binary operators; this group contains most of the operators that PHP supports, and a list follows below in the section Operator Precedence.

The third group is the ternary operator: ?:. It should be used to select between two expressions depending on a third one, rather than to select two sentences or paths of execution. Surrounding ternary expressions with parentheses is a very good idea.

算術運算符

還記得中小學教的加減乘除嗎 ? 這些算符就是那樣用的。

算術運算符
示範 算符名稱 運算結果
$a + $b 加法 Addition $a 、$b 的和
$a - $b 減法 Subtraction $a、 $b 的差
$a * $b 乘法 Multiplication $a 、$b 的積
$a / $b 除法 Division $a 、$b 的商
$a % $b 餘數 Modulus $a 除以 $b 後的餘數

The division operator ("/") returns an integer value (the result of an integer division) if the two operands are integers (or strings that get converted to integers) and the quotient is an integer. If either operand is a floating-point value, or the operation results in a non-integer value, a floating-point value is returned.