Page 47 - Open Soource Technologies 304.indd
P. 47

Unit 2: Language Basics



            Arithmetic negation (–)                                                               Notes
            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. Subtraction is binary and between its operands.
            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.
            String Concatenation Operator
            Manipulating string is such a core part of PHP applications that PHP has a separate string
            concatenation operator (.). The concatenation operator appends the right-hand operand to the
            left-hand operand and returns the resulting string. Operands are first converted to strings, if
            necessary. For example:
            $n = 5; $s = ‘There were ‘. $n. ‘ ducks.’; // $s is ‘There were 5 ducks’.

            Autoincrement and Autodecrement Operators
            In programming, one of the most common operations is to increase or decrease the value of a
            variable by one. The unary autoincrement (++) and autodecrement (––) operators provide shortcuts
            for these common operations. These operators are unique in that they work only on variables;
            the operators change their operands’ values as well as returning a value.
            There are two ways to use autoincrement or autodecrement in expressions. If you put the operator
            in front of the operand, it returns the new value of the operand (incremented or decremented).
            If you put the operator after the operand, it returns the original value of the operand (before the
            increment or decrement). Table 2.2 lists the different operations.
                          Table 2.2: Autoincrement and Autodecrement Operations

                        Operator      Name        Value returned  Effect on $var
                       $var++    Post-increment  $var           Incremented
                       ++$var    Pre-increment   $var + 1       Incremented
                       $var––    Post-decrement  $var           Decremented
                       ––$var    Pre-decrement   $var - 1       Decremented

            These operators can be applied to strings as well as numbers. Incrementing an alphabetic character
            turns it into the next letter in the alphabet. As illustrated in Table 2.3, incrementing “z” or “Z”
            wraps it back to “a” or “Z” and increments the previous character by one, as though the characters
            were in a base-26 number system.

                                  Table 2.3: Autoincrement with Letters

                                    Incrementing this  Gives this
                                    “a”               “b”
                                    “z”               “aa”
                                    “spaz”            “spba”
                                    “K9”              “L0”
                                    “42”              “43”





                                             LOVELY PROFESSIONAL UNIVERSITY                                    41
   42   43   44   45   46   47   48   49   50   51   52