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.
Open a new file in your text editor and type the following HTML:
<HTML> <HEAD> <TITLE>Printing Variables</TITLE> </HEAD> <BODY>
Add a PHP block and create a variable that holds an integer:
<? $intVar = "9554215464";
Create a variable that holds a floating-point number:
$floatVar = "1542.2232235";
Create a variable that holds a string:
$stringVar = "This is a string.";
Add an echo statement for each variable:
echo "<P>integer: $intVar</P>"; echo "<P>float: $floatVar</P>"; echo "<P>string: $stringVar</P>";
Close your PHP block and add some more HTML so that the document is valid:
?> </BODY> </HTML>
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.
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, those variables can be used only by the script in which they live. Scripts cannot magically reach inside other scripts and use the variables created and defined there—unless you say they can and you purposely 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, you will define them as able to be accessed by other scripts and functions, as needed.
You can learn about creating your own functions, and using global as well as local variables, in Appendix C, "Writing Your Own Functions and Objects." For now, just understand that there are two variable scopes—local and global—that come into play as you write more advanced scripts.
In all PHP scripts, a set of pre-defined variables is available to you. You might 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 pre-defined variables are called superglobals, meaning that they are always present and available to all of your scripts, without any intervention by you, the programmer.
Please study the following list of superglobals, because they will be used exclusively throughout this book. Each of these superglobals is actually an array of other variables. Don't worry about fully understanding this concept now, because it will be explained as you move through the book.
$_GET contains any variables provided to a script through the GET method.
$_POST contains any variables provided to a script through the POST method.
$_COOKIE contains any variables provided to a script through a cookie.
$_FILES contains any variables provided to a script through file uploads.
$_ENV contains any variables provided to a script as part of the server environment.
$_SESSION contains any variables that are registered in a session.
A constant is an identifier for a value that cannot change during the course of a script. Once a constant has a value, it remains through the constant's 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 names, and they are usually uppercase to show their difference from scalar variables. Next, you'll test 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.".
define("MYCONSTANT", "This is a test of defining constants.");
Print the value of the constant, and then close the PHP block:
echo MYCONSTANT;
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 these constants:
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:
echo "<br>This file is ".__FILE__;
| Note |
Concatenate means to add one string to the end of another, making a new string. |
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__;
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;
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; ?>
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.