Free downloading softwares and videos
  
Sender : Robert Bickford
Email : Robert_Bickford1976@yahoo.com
Monday, February 9, 2009 08:29:37 am

PHP Variable and Value Types

You will create two main types of variables in your PHP code: scalar and array. Scalar variables contain only one value at a time, and arrays contain a list of values, or even another array.The example at the beginning of this chapter created a scalar variable, and the code in this book deals primarily with scalar variables. You can find information on arrays in Appendix B, "Basic PHP Language Reference." When you assign a value to a variable, you usually assign a value of one of the following types:

  • Integers. Whole numbers (numbers without decimals). Examples are 1,345 and 9922786. You can also use octal and hexadecimal notation: the octal 0123 is decimal 83 and the hexadecimal 0x12 is decimal 18.

  • Floating-point numbers ("floats" or "doubles"). Numbers with decimals. Examples are 1.5, 87.3446, and 0.88889992.

  • Strings. Text and/or numeric information, specified within double quotes (" ") or single quotes (’ ’).

As you begin your PHP script, plan your variables and variable names carefully, and use comments in your code to remind yourself of the assignments you have made.

Create a simple script that assigns values to different variables and then simply prints the values to the screen.

  1. Open a new file in your text editor and type the following HTML:

    <HTML>
        <HEAD>
        <TITLE>Printing Variables</TITLE>
        </HEAD>
        <BODY>
        
  2. Add a PHP block and create a variable that holds an integer:

    <?
        $intVar = "9554215464";
        
  3. Create a variable that holds a floating-point number:

    $floatVar = "1542.2232235";
        
        
  4. Create a variable that holds a string:

    $stringVar = "This is a string.";
        
  5. Add an echo statement for each variable:

    echo "<P>integer: $intVar</P>";
        echo "<P>float: $floatVar</P>";
        echo "<P>string: $stringVar</P>";
        
  6. Close your PHP block and add some more HTML so that the document is valid:

    ?>
        </BODY>
        </HTML>
        
  7. Save the file with the name printvarscript.php and place this file in the document root of your Web server.

  8. Open your Web browser and type http://127.0.0.1/printvarscript.php  

    You can see by this output that the values you assigned to the variables $intVar, $floatVar, and $stringVar were the values printed to the screen. In the next section, you’ll learn how to use operators to change the values of your variables.

Local and Global Variables

Variables can be local or global, the difference having to do with their definition and use by the programmer, and where they appear in the context of the scripts you are creating. The variables described in the previous section, and for the majority of this book, are local variables.

When you write PHP scripts that use variables (and likely they always will), these variables can be used only by the script they live within. Scripts cannot magically reach inside other scripts and use the variables created and defined within them-unless you say they can and you purposefully link them together. When you do just that, such as when you create your own functions (blocks of reusable code that perform a particular task), you will define the shared variables as global. That is, able to be accessed by other scripts and functions which need them.

You can learn about creating your own functions, and using global as well as local variables, in Appendix C, "Writing Your Own Functions." For now, just understand that there are two different variable scopes-local and global-that will come into play as you write more advanced scripts.

Predefined Variables

In all PHP scripts, a set of predefined variables are in use. You may have seen some of these variables in the output of the phpinfo() function, if you scrolled and read through the entire results page. Some of these predefined variables are also called superglobals, essentially meaning that they are always present and available in your scripts.

Please study the following list of superglobals, as they will be used exclusively throughout this book. Each of these superglobals are actually arrays of other variables. Don’t worry about fully understanding this concept now; it will be explained as you move along through the book.

  • $_GET Any variables provided to a script through the GET method.

  • $_POST Any variables provided to a script through the POST method.

  • $_COOKIE Any variables provided to a script through a cookie.

  • $_FILES Any variables provided to a script through file uploads.

  • $_ENV Any variables provided to a script as part of the server environment.

  • $_SESSION Any variables which are currently registered in a session.

Note 

If you are using a version of PHP earlier than 4.1.x and cannot upgrade to a newer version of PHP as described in Chapter 3, "Installing PHP," you must adjust the names of these variables when following the scripts in this book. The old names are as follows: $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_COOKIE_VARS, $HTTP_POST_FILES, $HTTP_ENV_VARS, and $HTTP_SESSION_VARS.

Using Constants

A constant is an identifier for a value that cannot change during the course of a script. Once it has a value, it remains through its execution lifetime. Constants can be user-defined, or you can use some of the predefined constants that PHP always has available. Unlike simple variables, constants do not have a dollar sign before their name, and they are usually uppercase to show their difference from a scalar variable. Next, you’ll test out the user-defined type.

  1. Open a new file in your text editor and open a PHP block:

    <?
        
  2. The function used to define a constant is called define() and it requires the name of the constant and the value you want to give it. Here you define a constant called MYCONSTANT with a value of "This is a test of defining constants."

    define("MYCONSTANT", "This is a test of defining constants.");
        
  3. Print the value of the constant, then close the PHP block:

    echo MYCONSTANT;
        
  4. Save the file with the name constants.php and place this file in the document root of your Web server.

  5. Open your Web browser and type http://127.0.0.1/constants.php  

    Some predefined constants include

    • _FILE_ The name of the script file being parsed

    • _LINE_ The number of the line in the script being parsed

    • PHP_VERSION The version of PHP in use

    • PHP_OS The operating system using PHP

Let’s test them all out:

  1. Open a new file in your text editor and open a PHP block:

    <?
        
        
  2. Use the echo statement to display an introductory string, and concatenate the _FILE_ constant to the end of it:

    echo "<br>This file is "._FILE_;
        
  3. Use the echo statement to display an introductory string, and concatenate the _LINE_ constant to the end of it:

    echo "<br>This is line number "._LINE_;
        
  4. Use the echo statement to display an introductory string, and concatenate the PHP_VERSION constant to the end of it:

    echo "<br>I am using ".PHP_VERSION;
        
  5. Use the echo statement to display an introductory string, and concatenate the PHP_OS constant to the end of it. Also close up the PHP block:

    echo "<br>This test is being run on ".PHP_OS;
        ?>
        
  6. Save the file with the name constants2.php and place this file in the document root of your Web server.

  7. Open your Web browser and type http://127.0.0.1/constants2.php  

    You should see the strings you typed, plus the values of the constants. Your values will likely differ from those you see here.

[ Rating : rating ]
Print Send Comment
:: COPPYRIGHT 2005 - 2008 | WWW.THILOSOFT.COM | ® ALL RIGHTS RESERVED ::
:: CONTACT WEBMASTER - WEBMASTER@THILOSOFT.COM ::
:: MOBILE - +94 - 777448623 ::