All expressions consist of at least one operand and one or more operators. A few examples follow:
$a = 5; // assign integer value 5 to the variable $a
$a = "5"; // assign string value "5" to the variable $a
$sum = 50 + $some_int; // assign sum of 50 + $some_int to $sum
$wine = "Zinfandel"; // assign "Zinfandel" to the variable $wine
$inventory++; // increment the variable $inventory by 1
$a = "5"; // assign string value "5" to the variable $a
$sum = 50 + $some_int; // assign sum of 50 + $some_int to $sum
$wine = "Zinfandel"; // assign "Zinfandel" to the variable $wine
$inventory++; // increment the variable $inventory by 1
Operands
Operands are the inputs of an expression(variables, constants, etc.) You might already be familiar with the manipulation and use of operands not only through everyday mathematical calculations, but also through prior programming experience. Some examples of operands follow:
$a++; // $a is the operand
$sum = $val1 + val2; // $sum, $val1 and $val2 are operands
$sum = $val1 + val2; // $sum, $val1 and $val2 are operands
Operators
An operator is a symbol that specifies a particular action in an expression(+, –, !, &&, etc.). Many operators may be familiar to you. Regardless, you should remember that PHP’s automatic type conversion will convert types based on the type of operator placed between the two operands, which is not always the case in other programming languages.
The precedence and associativity of operators are significant characteristics of a programming language. I’ll introduce both here. Table below contains a complete listing of all operators, ordered from highest to lowest precedence.
Operator
|
Associativity
|
Purpose
|
new | NA | Object instantiation |
( ) | NA | Expression subgrouping |
[ ] | RIGHT | Index enclosure |
! ~ ++ -- | RIGHT | Boolean NOT, bitwise NOT, increment, decrement |
@ | RIGHT | Error suppression |
/ * % | LEFT | Division, multiplication, modulus |
+ - . | LEFT | Addition, subtraction, concatenation |
<< >> | LEFT | Shift left, shift right (bitwise) |
< <= > >= | NA | Less than, less than or equal to, greater than, greater than or equal to |
== != === <> | NA | Is equal to, is not equal to, is identical to, is not equal to |
& ^ | | LEFT | Bitwise AND, bitwise XOR, bitwise OR |
&& || | LEFT | Boolean AND, Boolean OR |
?: | RIGHT | Ternary operator |
= += *= /= .= %=&= |= ^= <<= >>= | RIGHT | Assignment operators |
AND XOR OR | LEFT | Boolean AND, Boolean XOR, Boolean OR |
, | LEFT | Expression separation |
Operator Precedence, Associativity, and Purpose
Operator Precedence
Operator precedence is a characteristic of operators that determines the order in which they evaluate the operands surrounding them. PHP follows the standard precedence rules used in elementary school math class. Consider a few examples:
$total_cost = $cost + $cost * 0.06;
This is the same as writing
$total_cost = $cost + ($cost * 0.06);
because the multiplication operator has higher precedence than the addition operator. So first $cost is multiplied to 0.06 and then the result is added to $cost and the value achieved is finally assigned to variable $total_cost.
Operator Associativity
The associativity characteristic of an operator specifies how operations of the same precedence (i.e., having the same precedence value, as displayed in Table above) are evaluated as they are executed. Associativity can be performed in two directions, left-to-right or right-to-left. Left-to-right associativity means that the various operations making up the expression are evaluated from left to right. Consider the following example:
$value = 3 * 4 * 5 * 7 * 2;
The preceding example is the same as the following:
$value = ((((3 * 4) * 5) * 7) * 2);
This expression results in the value 840 because the multiplication (*) operator is left-to-right associative.
In contrast, right-to-left associativity evaluates operators of the same precedence from right to left:
$c = 5;
$value = $a = $b = $c;
$value = $a = $b = $c;
The preceding example is the same as the following:
$c = 5;
$value = ($a = ($b = $c));
$value = ($a = ($b = $c));
When this expression is evaluated, variables $value, $a, $b, and $c will all contain the value 5 because the assignment operator (=) has right-to-left associativity.
Arithmetic Operators
The arithmetic operators, listed in Table below, perform various mathematical operations and will probably be used frequently in many of your PHP programs. Fortunately, they are easy to use. Incidentally, PHP provides a vast assortment of predefined mathematical functions capable of performing base conversions and calculating logarithms, square roots, geometric values, and more. Check the manual on php website for an updated list of these functions.
Example
|
Label
|
Outcome
|
$a + $b |
Addition
|
Sum of $a and $b
|
$a - $b |
Subtraction
|
Difference of $a and $b
|
$a * $b |
Multiplication
|
Product of $a and $b
|
$a / $b |
Division
|
Quotient of $a and $b
|
$a % $b |
Modulus
|
Remainder of $a divided by $b
|
Arithmetic Operators
0 comments:
Post a Comment