Приглашаем посетить
Некрасов (nekrasov-lit.ru)

Section 6.8.  Some Operator Examples

Previous
Table of Contents
Next

6.8. Some Operator Examples

Here are some examples of most of these operators in action:

    $somevar = 5 + 5; // 10
    $somevar = 5 - 5; // 0
    $somevar = 5 + 5 - (5 + 5); // 0
    $somevar = 5 * 5; // 25
    $somevar = 10 * 5 - 5; // 45
    $somevar = $somevar . "appended to end";
    $somevar = false;
    $somevar = !$somevar; // $somevar is now set to true
    $somevar = 5;
    $somevar++; // $somevar is now 6
    $somevar--; // $somevar is now 5 again
    ++$somevar; // $somevar is 6

The third line uses parentheses to control the order of operations. This is important, as the equation 5 + 5 - 5 + 5 can be taken in more than one way, such as 5 + (5 - 5) + 5, which is 10. There are some equations, such as the one on line five, where parentheses are not needed. There, 10 * 5 - 5 can only be taken to mean (10 * 5) - 5 because of the mathematical rules of precedence (rules of operations)multiplication is considered higher in order (executed first) than subtraction.

Despite each operator having specific precedence, it is still best to use parentheses in order to make your meaning clear. Expressions inside parentheses are always evaluated first, and you can use any number of parentheses in order to get the expression correct.


Previous
Table of Contents
Next