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

Unit 6: Building Blocks of PHP



            6.1.4 Expressions                                                                     Notes

            Expressions are the most important building stones of PHP. In PHP, almost anything you write
            is an expression. The simplest yet most accurate way to define an expression is “anything that
            has a value”.

            The most basic forms of expressions are constants and variables. When you type “$a = 5”, you’re
            assigning ‘5’ into $a. ‘5’, obviously, has the value 5, or in other words ‘5’ is an expression with
            the value of 5 (in this case, ‘5’ is an integer constant).

            After this assignment, you’d expect $a’s value to be 5 as well, so if you wrote $b = $a, you’d
            expect it to behave just as if you wrote $b = 5. In other words, $a is an expression with the value
            of 5 as well. If everything works right, this is exactly what will happen.

            Slightly more complex examples for expressions are functions. For instance, consider the
            following function:

            < ?p hp
            function foo ()

            {
               return 5;

            }
            ? >
            Assuming you’re familiar with the concept of functions (if you’re not, take a look at the unit
            about functions), you’d assume that typing $c = foo() is essentially just like writing $c = 5, and
            you’re right. Functions are expressions with the value of their return value. Since foo() returns
            5, the value of the expression ‘foo()’ is 5. Usually functions don’t just return a static value but
            compute something.

            Of course, values in PHP don’t have to be integers, and very often they aren’t. PHP supports
            four scalar value types: integer values, floating point values (float), string values and boolean
            values (scalar values are values that you can’t ‘break’ into smaller pieces, unlike arrays, for
            instance). PHP also supports two composite (non-scalar) types: arrays and objects. Each of these
            value types can be assigned into variables or returned from functions.

            PHP takes expressions much further, in the same way many other languages do. PHP is an
            expression-oriented language, in the sense that almost everything is an expression. Consider the
            example we’ve already dealt with, ‘$a = 5’. It’s easy to see that there are two values involved here,
            the value of the integer constant ‘5’, and the value of $a which is being updated to 5 as well. But
            the truth is that there’s one additional value involved here, and that’s the value of the assignment
            itself. The assignment itself evaluates to the assigned value, in this case 5. In practice, it means that
            ‘$a = 5’, regardless of what it does, is an expression with the value 5. Thus, writing something like
            ‘$b = ($a = 5)’ is like writing ‘$a = 5; $b = 5;’ (a semicolon marks the end of a statement). Since
            assignments are parsed in a right to left order, you can also write ‘$b = $a = 5’.
            Another  good  example  of  expression  orientation is  pre- and  post-increment and  decrement.
            Users of PHP and many other languages may be familiar with the notation of variable++ and




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