Page 72 - Beginning PHP 5.3
P. 72

Part II: Learning the Language
                   As you might imagine, this code outputs the number  4  when it ’ s run. This is all well and good; however,
                 if you wanted to print the value of, say, 5 + 6 instead, you ’ d have to write another PHP script, as follows:
                    echo 5 + 6;

                   This is where variables come into play. By using variables instead of numbers in your script, you make
                 the script much more useful and flexible:

                    echo $x + $y;

                   You now have a general - purpose script. You can set the variables  $x  and  $y  to any two values you want,
                either at some other place in your code, or as a result of input from the user. Then, when you run the
                preceding line of code, the script outputs the sum of those two values. Re - run the script with different
                values for   $x  and  $y , and you get a different result.

                  Naming Variables

                   A variable consists of two parts: the variable ’ s name and the variable ’ s value. Because you ’ ll be using
                 variables in your code frequently, it ’ s best to give your variables names you can understand and
                 remember. Like other programming languages, PHP has certain rules you must follow when naming
                 your variables:

                   ❑       Variable names begin with a dollar sign (  $ )
                   ❑       The first character after the dollar sign must be a letter or an underscore
                   ❑       The remaining characters in the name may be letters, numbers, or underscores without a
                       fixed limit

                  Variable names are case - sensitive (  $Variable  and  $variable  are two distinct variables), so it ’ s worth
                sticking to one variable naming method  —  for example, always using lowercase  —  to avoid mistakes.
                It ’ s also worth pointing out that variable names longer than 30 characters are somewhat impractical.

                  Here are some examples of PHP variable names:
                    $my_first_variable
                    $anotherVariable
                    $x


                    $_123
                  Creating Variables

                   Creating a variable in PHP is known as  declaring  it. Declaring a variable is as simple as using its name in
                 your script:

                    $my_first_variable;
                   When PHP first sees a variable ’ s name in a script, it automatically creates the variable at that point.





              34





                                                                                                      9/21/09   8:51:21 AM
          c03.indd   34
          c03.indd   34                                                                               9/21/09   8:51:21 AM
   67   68   69   70   71   72   73   74   75   76   77