The Building Blocks: Data Types, Literals, Variables, and Constants - Part 2/Page 4
[previous] [next]
The Building Blocks:
Data Types, Literals, Variables, and Constants - Part 2
Scope of Variables
Scope refers to where a variable is available within a script. Scope is important because it prevents important variables from being accidentally modified in some other part of the program and thus changing the way the program behaves. PHP has specific rules to control the visibility of variables. A local variable is one that exists only within a function. A global variable is available anywhere in the script other than from within functions. (See Chapter 9, "User-Defined Functions," for creating global variables within functions.) For the most part all PHP variables have a single scope, global.
Local Variables. Variables created within a function are not available to the rest of the script. They are local to the function and disappear (go out of scope) when the function exits. If you have the same name for a variable within a function as in the main program, modifying the variable in the function will not affect the one outside the function (see Chapter 9, "User-Defined Functions"). Likewise, the function does not have access to variables created outside of the function. Most of the variables we create will be visible in the script or function in which they are declared. See Chapter 9, "User-Defined Functions," to get a full understanding of scope, including local variables, globals, superglobals, and static.
Global and Environment Variables. Superglobal variables (see Table 4.4) are accessible everywhere within a script and within functions. They are special variables provided by PHP to help you manage HTML forms, cookies, sessions, and files, and to get information about your environment and server.
Table 4.4 Some Superglobal Variables
$GLOBALS | An array of all global variables |
$_SERVER | Contains server variables (e.g., REMOTE_ADDR) |
$_GET | Contains form variables sent through GET method |
$_POST | Contains form variables sent through POST method |
$_COOKIE | Contains HTTP cookie variables |
$_FILES | Contains variables provided to the script via HTTP post file uploads |
$_ENV | Contains the environment variables |
$_REQUEST | A merge of the GET variables, POST variables, and cookie variables |
$_SESSION | Contains HTTP variables registered by the session module |
Managing Variables
You might want to find out if a variable has been declared, you might want to delete one that has been set, or check to see if one that is set is not empty or is a string, number, scalar, and so on. PHP provides a number of functions (see Table 4.5) to help you manage variables.
Table 4.5 Functions for Managing Variables
Function | What It Returns |
isset() | True if variable has been set. |
empty() | True if variable is empty: "" (an empty string); "0" (a string); 0 (an integer) |
is_bool() | True if variable is boolean; that is, contains TRUE or FALSE. |
is_callable() | True if variable is assigned the name of a function or an object. |
is_double(), is_float(), is_real() | True if variable is a floating-point number (e.g., 5.67 or .45). |
is_int, is_integer, is_long | True if a variable is assigned a whole number. |
is_null() | True if a variable was assigned the NULL value. |
is_numeric() | True if the variable was assigned a numeric string value or a number. |
is_object() | True if the variable is an object. |
is_resource() | True if the variable is a resource. |
is_scalar() | True if the value was assigned a single value, such as a number (e.g., "555" a string, or a boolean, but not an array or object). |
is_string() | True if a variable is a string of text (e.g., "hello"). |
unset() | Unsets or destroys a list of values. |
The isset() Function. The isset()
function returns true if a variable has been set and false if it hasn't. If the variable has been set to NULL
or has no value, it returns false. If you want to see if a variable has been set to NULL
, use the is_null()
function. To ensure that a variable has an initial value, the isset()
function can be used to set a default value. See Examples 4.17 and 4.18.
Format
Example 4.17
Explanation
|
Example 4.18
Explanation
|
The empty() Function. The empty()
function returns true if a variable does not exist, or exists and has been assigned one of the following: an empty string " ", 0
as a number, "0
" as a string, NULL
, or no value at all. Example 4.19 demonstrates the use of the empty()
function.
Format
Example 4.19
The unset() Function. The unset()
function (technically a language construct) unsets or destroys a given variable. It can take a varied number of arguments and behaves a little differently within functions (see Chapter 9, "User-Defined Functions"). As of PHP 4, it has no return value and is considered a statement.
Format
Example 4.20
Explanation
|
[previous] [next]
URL: