php-unit-1-operator-and-expression_part-2
php-unit-1-operator-and-expression_part-2
Addition (+)
The result of the addition operator is the sum of the two operands.
Subtraction (-)
The result of the subtraction operator is the difference between the two operands; i.e., the value of the
second operand subtracted from the first.
Multiplication (*)
The result of the multiplication operator is the product of the two operands.
For example, 3 * 4 is 12.
Division (/)
The result of the division operator is the quotient of the two operands. Dividing two integers can give
an integer (e.g., 4/2) or a floating-point result (e.g., 1/2).
Modulus (%)
The modulus operator converts both operands to integers and returns the remainder of the division of
the first operand by the second operand. For example, 10% 6 is 4.
Arithmetic negation (-)
The arithmetic negation operator returns the operand multiplied by -1, effectively changing its sign. For
example, -(3 - 4) evaluates to 1.
Arithmetic negation is different from the subtraction operator, even though they both are written as a
minus sign.
Arithmetic negation is always unary and before the operand.
Arithmetic assertion (+)
The arithmetic assertion operator returns the operand multiplied by +1, which has no effect. It is used
only as a visual cue to indicate the sign of a value.
For example, +(3 - 4) evaluates to -1, just as (3 - 4) does.
Plus-equals (+=)
Adds the righthand operand to the value of the lefthand operand, then assigns the result to the lefthand
operand. $a += 5 is the same as $a = $a + 5.
Minus-equals (-=)
Subtracts the righthand operand from the value of the lefthand operand, then assigns the result to the
lefthand operand.
Divide-equals (/=)
Divides the value of the lefthand operand by the righthand operand, then assigns the result to the
lefthand operand.
Multiply-equals (*=)
Multiplies the righthand operand with the value of the lefthand operand, then assigns the result to the
lefthand operand.
Modulus-equals (%=)
Performs the modulus operation on the value of the lefthand operand and the righthand operand, then
assigns the result to the lefthand operand.
Bitwise-XOR-equals (^=)
Performs a bitwise XOR on the lefthand and righthand operands, then assigns the result to the lefthand
operand.
Bitwise-AND-equals (&=)
Performs a bitwise AND on the value of the lefthand operand and the righthand operand, then assigns
the result to the lefthand operand.
Bitwise-OR-equals (|=)
Performs a bitwise OR on the value of the lefthand operand and the righthand operand, then assigns the
result to the lefthand operand.
Concatenate-equals (.=)
Concatenates the righthand operand to the value of the lefthand operand, then assigns the result to the
lefthand operand.
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
String that is entirely numeric String that is not entirely numeric Lexicographic
String that is not entirely numeric String that is not entirely numeric Lexicographic
PHP has two operators that are specially designed for strings.
Use the PHP functions bindec( ), decbin( ), octdec( ), and decoct( ) to convert numbers back and forth
to understand binary arithmetic.
For example, "wolf" & "cat" is "cad".
Bitwise OR (|)
The bitwise OR operator compares each corresponding bit in the binary representations of the operands.
If both bits are 0, the resulting bit is 0; otherwise, the resulting bit is 1.
For example, 0755 | 020 is 0775.
For example, "pussy" | "cat" is "suwsy".
Casting affects the way other operators interpret a value, rather than changing the value in a
variable. For example, the code:
$a = "5";
$b = (int) $a;
assigns $b the integer value of $a; $a remains the string "5". To cast the value of the variable
itself, you must assign the result of a cast back into the variable:
$a = "5"
$a = (int) $a; // now $a holds an integer
9. PHP Miscellaneous Operators
The remaining PHP operators are for error suppression, executing an external command, and
selecting values:
i. Error suppression (@)
Some operators or functions can generate error messages.
The error suppression operator is used to prevent these messages from being created.
ii. Execution (`...`)
The backtick operator executes the string contained between the backticks as a shell command
and returns the output.
For example: $listing = `ls -ls /tmp`;
echo $listing;
iii. Conditional (?:)
The conditional operator is, depending on the code you look at, either the most overused or most
underused operator.
It is the only ternary (three-operand) operator and is therefore sometimes just called the ternary
operator.
The conditional operator evaluates the expression before the ?. If the expression is true, the
operator returns the value of the expression between the ? and :; otherwise, the operator returns
the value of the expression after the :.
For instance:
<a href="<?= $url ?>"><?= $linktext ? $linktext : $url ?></a>