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

Unit 6: Building Blocks of PHP



            6.1.3.8 Increment/Decrement Operators                                                 Notes
            Increment/decrement operators are unique in the sense that they operate only on variables and
            not on any value. The reason for this is that in addition to calculating the result value, the value
            of the variable itself changes as well.

               Operator   Name           Effect on $var       Value of the Expression
               $var++     Post-increment  $var is incremented by 1. The previous value of $var.


               ++$var     Pre-increment  $var is incremented by 1. The new value of $var
                                                              (incremented by 1).
               $var––     Post-decrement  $var is decremented by  The previous value of $var.
                                         1.
               ––$var     Pre-decrement  $var is decremented by  The new value of $var
                                         1.                   (decremented by 1).

            As you can see from the previous table, there’s a difference in the value of post and pre increment.
            However, in both cases, $var is incremented by 1. The only difference is in the value to which
            the increment expression evaluates.

                  Example: 1:
                        $num1 = 5;

                        $num2 = $num1++;// post-increment, $num2 is assigned $num1’s original value

                        print $num1; // this will print the value of $num1, which is now 6
                        print $num2; // this will print the value of $num2, which is the

                        →  original value of $num1, thus, 5

                  Example: 2:
                        $num1 = 5;

                        $num2 = ++$num1;// pre-increment, $num2 is assigned $num1’s

                        →  incremented value
                        print $num1; // this will print the value of $num1, which is now 6

                        print $num2; // this will print the value of $num2, which is the
                        →  same as the value of $num1, thus, 6

            The same rules apply to pre- and post-decrement.
            Incrementing Strings Strings (when not numeric) are incremented in a similar way to Perl. If
            the last letter is alphanumeric, it is incremented by 1. If it was ‘z’, ‘Z’, or ‘9’, it is incremented
            to ‘a’, ‘A’, or ‘0’ respectively, and the next alphanumeric is also incremented in the same way.
            If there is no next alphanumeric, one is added to the beginning of the string as ‘a’, ‘A’, and ‘1,’
            respectively. If this gives you a headache, just try and play around with it.




                                             LOVELY PROFESSIONAL UNIVERSITY                                    91
   91   92   93   94   95   96   97   98   99   100   101