The Building Blocks: Data Types, Literals, Variables, and Constants - Part 2/Page 4 | WebReference

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

  1. Three variables are assigned string values; $middle_name is assigned the empty string, a set of double quotes containing no text.
  2. $ageis declared but has not been assigned any value yet, which evaluates to NULL.
  3. $state is declared and assigned the value NULL, which means it has no value.
  4. The isset() function returns true if a variable has been set and given a non-null value. In this case, all three variables have a value, even the variable assigned an empty string. If true, 1 is displayed; if false, 0 or nothing is displayed.
  5. Because $age was not given any value, it is implied to be null, and isset() returns false. $city was never even declared, and $state was assigned NULL; isset() returns false. If you want to check explicitly for the NULL value (case insensitive), use the built-in is_null() function (see Figure 4.19).

Figure 4.19 Is the variable set? Output from Example 4.17.

Example 4.18

Explanation

  1. The isset() function returns true if $temp has been set. The ! operator, the unary "not" operator, reverses the boolean result. The expression reads, "if $temp is not set, define it with a value of 68."
  2. The echo statement displays the default value in the browser (see Figure 4.20).

Figure 4.20 Setting a default value. Output from Example 4.18.

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

Figure 4.21 Is the variable empty? Output from 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

  1. Two scalar variables are declared and assigned string values.
  2. The built-in unset() function will destroy the variables listed as arguments.
  3. The isset() function returns true if a variable exists, and false if it doesn't.

Figure 4.22 Destroying variables.


[previous] [next]

URL: