-
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.
-
Open a new file in your text editor and type the following HTML:
-
Add a PHP block and create a variable that holds an integer:
-
Create a variable that holds a floating-point number:
-
Create a variable that holds a string:
-
Add an echo statement for each variable:
-
Close your PHP block and add some more HTML so that the document is valid:
-
Save the file with the name printvarscript.php and place this file in the document root of your Web server.
-
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.
-
Open a new file in your text editor and open a PHP block:
-
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."
-
Print the value of the constant, then close the PHP block:
-
Save the file with the name constants.php and place this file in the document root of your Web server.
-
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:
-
Open a new file in your text editor and open a PHP block:
-
Use the echo statement to display an introductory string, and concatenate the _FILE_ constant to the end of it:
-
Use the echo statement to display an introductory string, and concatenate the _LINE_ constant to the end of it:
-
Use the echo statement to display an introductory string, and concatenate the PHP_VERSION constant to the end of it:
-
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:
-
Save the file with the name constants2.php and place this file in the document root of your Web server.
-
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.
|