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

Open Source Technologies



                   Notes         variable—These are increment and decrement operators. In PHP/FI 2, the statement ‘$a++’ has
                                 no value (is not an expression), and thus you can’t assign it or use it in any way. PHP enhances
                                 the increment/decrement capabilities by making these expressions as well, like in C. In PHP, like
                                 in C, there are two types of increment-pre-increment and post-increment. Both pre-increment
                                 and post-increment essentially increment—the variable, and the effect on the variable is identical.
                                 The difference is with the value of the increment expression. Pre-increment, which is written
                                 ‘++$variable’, evaluates to the incremented value (PHP increments the variable before reading its
                                 value, thus the name ‘pre-increment’). Post-increment, which is written ‘$variable++’ evaluates
                                 to the original value of $variable, before it was incremented (PHP increments the variable after
                                 reading its value, thus the name ‘post-increment’).
                                 A very common type of expressions are comparison expressions. These expressions evaluate to
                                 either FALSE or TRUE. PHP supports > (bigger than), >= (bigger than or equal to), == (equal),
                                 != (not equal), < (smaller than) and <= (smaller than or equal to). The language also supports
                                 a set of strict equivalence operators: === (equal to and same type) and !== (not equal to or not
                                 same type). These expressions are most commonly used inside conditional execution, such as
                                 if statements.

                                 The last example of expressions we’ll deal with here is combined operator-assignment expressions.
                                 You already know that if you want to increment $a by 1, you can simply write ‘$a++’ or ‘++$a’.
                                 But what if you want to add more than one to it, for instance 3? You could write ‘$a++’ multiple
                                 times, but this is obviously not a very efficient or comfortable way. A much more common practice
                                 is to write ‘$a = $a + 3’. ‘$a + 3’ evaluates to the value of $a plus 3, and is assigned back into
                                 $a, which results in incrementing $a by 3. In PHP, as in several other languages like C, you can
                                 write this in a shorter way, which with time would become clearer and quicker to understand
                                 as well. Adding 3 to the current value of $a can be written ‘$a += 3’. This means exactly “take
                                 the value of $a, add 3 to it, and assign it back into $a”. In addition to being shorter and clearer,
                                 this also results in faster execution. The value of ‘$a += 3’, like the value of a regular assignment,
                                 is the assigned value. Notice that it is NOT 3, but the combined value of $a plus 3 (this is the
                                 value that’s assigned into $a). Any two-place operator can be used in this operator-assignment
                                 mode, for example ‘$a -= 5’ (subtract 5 from the value of $a), ‘$b *= 7’ (multiply the value of
                                 $b by 7), etc.

                                 There is one more expression that may seem odd if you haven’t seen it in other languages, the
                                 ternary conditional operator:

                                 <?php
                                 $first ? $second : $third

                                 ?>
                                 If the value of the first subexpression is TRUE (non-zero), then the second subexpression is
                                 evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression
                                 is evaluated and that is the value.

                                 The following example should help you understand pre- and post-increment and expressions
                                 in general a bit better:

                                 <?php
                                 function double($i)


        94                                LOVELY PROFESSIONAL UNIVERSITY
   94   95   96   97   98   99   100   101   102   103   104