Page 88 - Beginning PHP 5.3
P. 88

Part II: Learning the Language
                  Then the script calculates the circle ’ s area, which is  π  times the radius squared, and stores it in an   $area
                variable. To get the value of the radius squared, the script uses the built - in   pow()  function, which takes
                a base number,   base , followed by an exponent,  exp , and returns  base  to the power of  exp .
                  Finally, the script outputs the results of its calculations, using the string concatenation operator (  . ) to
                join strings together.



                  Summary

                   This chapter took you through some fundamental building blocks of the PHP language. You learned the
                 following concepts:
                   ❑       Variables: What they are, how you create them, and how to name them
                   ❑       The concept of data types, including the types available in PHP

                   ❑       Loose typing in PHP; a feature that gives you a lot of flexibility with variables and values
                   ❑       How to test the type of a variable with   gettype() , and how to change types with  settype()
                       and casting
                   ❑       The concepts of operators, operands, and expressions
                   ❑       The most common operators used in PHP
                   ❑       Operator precedence  —  all operators are not created equal

                   ❑       How to create constants that contain non - changing values

                   Armed with this knowledge, you ’ re ready to move on and explore the next important concepts of PHP:
                 decisions, loops, and control flow. You learn about these in the next chapter. Before you read it, though,
                 try the two exercises that follow to ensure that you understand variables and operators. You can find the
                 solutions to these exercises in Appendix A.


                  Exercises

                      1.       Write a script that creates a variable and assigns an integer value to it, then adds 1 to the variable ’ s
                       value three times, using a different operator each time. Display the final result to the user.
                      2.       Write a script that creates two variables and assigns a different integer value to each variable.
                       Now make your script test whether the first value is
                           a.   equal to the second value


                           b.   greater than the second value
                           c.   less than or equal to the second value


                           d.   not equal to the second value
                         and output the result of each test to the user.



















              50
                                                                                                      9/21/09   8:51:27 AM
          c03.indd   50                                                                               9/21/09   8:51:27 AM
          c03.indd   50
   83   84   85   86   87   88   89   90   91   92   93