| [main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql] | |
Note that all external links will open up in a separate window. This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser. |
VariablesThese tutorials are about PHP and its use for server-side Web programming. PHP Variablesvariable: a name, or identifier, associated with a value A variable is just a name assigned to reference a location in memory where some value is stored. It is a shortcut, so that you don't have to refer to memory addresses directly. Since variables are meant to be a people friendly versions of memory locations, we like to pretend that the variables in our programs actually store data. Thinking in computerese is just a little bit easier that way. But you should still know the difference. All variables in PHP are prefixed with a dollar sign. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP parser to recognize the variable as such. Declaring Variables
PHP has no command for declaring a variable. A variable is created the moment you first assign a value to it. Setting a variable functions as its declaration. If you want to create a variable without assigning it a value, then you assign it the value of
If you try to read the value of a non-existent variable the value also equates to
The Variable Variables and Variable ReferencesPHP allows you to do some neat things with variables. It allows you to create aliases for variables, and it also allows you to have variables whose name is a variable.
A variable reference, or alias, is a variable assigned to refer to the same information as another variable. To assign an alias to a variable, you use the reference operator, which is an equals sign followed by an ampersand ( $that = 'abc'; // $that now equals 'abc' $this =& $that; // $this now equals 'abc'; $that = 'def'; // both now equal 'def'; $this = 'ghi'; // both now equal 'ghi';
Since they are both now references for the same location in memory, changing the value of one, changes the value of the other. Note however that if you
You can also use references with functions to return values by reference instead of by value. To do this, you precede the name of the function with an ampersand ( Variable variables are variables whose name is a variable. In other words, the name of the variable refers to a memory location that stores the name of the variable that stores the information you want. (Hope you paid attention to pointers in your whatever programming 101 course you learned C or C++ in.) They are represented by adding an additional dollar sign in front of the variable so that the name gets parsed twice. $foo_bar = "abc"; $tip = "foo_bar"; echo $tip; // writes out 'foo_bar' echo $$tip // writes out 'abc' Sometimes curly brackets are used to make the code a little clearer. Curly brackets, in PHP are grouping elements. By using them with a variable name, you can make it clear what each dollar sign is acting on in the variable name. This makes the code easier to read.
$$tip // harder to read
${$tip} // easier to read
Variable Scopescope: is the region of the program for which the variable is declared All variables have scope. The scope of a variable is the portion of the script in which the variable can be referenced. PHP has four different variable scopes.
In PHP, all variables are local in scope, even the global ones. Anyone who knows what global and local scopes are can now do a double take. What that means is that all global variables not explicitly passed to a function are not accessible to that function. Local ScopeA local variable is on that is specific to a given instance of a given function. It is created when the function processing begins and is deleted as soon as the function is completed. Local scope is specific to functions. PHP does not have a block level scope. If you want a block level scope, your best bet is to emulate it with a recursive function. Global ScopeGlobal scope refers to any variable that is defined outside of any function. They can be accessed from any part of the program that is not inside a function.
To access a global variable from within a function, you can call it into the function with the global $varToInclude;
PHP also stores all global variables in an array called global $somVar; $someVar = 'abc' // is the same as $GLOBALS["someVar"] = 'abc'; Static Scope
Normally when a function terminates, all of its variables are also cleaned up. Sometimes you want a local variable to persist between instances of a given function. To do this you use the static $aValueToRemember; Even though the value persists, the variable is still local to the function. ParametersA parameter is a local variable whose value is passed to the function by the calling code. Unlike other variables, parameters are declared in a parameter list as part of the function declaration.
function myFunc($para1, $para2, [...]) {
// function code
}
Parameters are also sometimes called arguments. We will discuss them in more detail when we talk about functions. Environment Variables
Beyond the variables you declare in your code, PHP has a collection of environment variables, which are system defined variables that are accessible from anywhere inside the PHP code, inside of functions or out. We already encountered one in the All of these environment variables are stored by PHP as arrays. Some you can address directly by using the name of the index position as a variable name. Other can only be accessed through their arrays. Which can be accessed which way is heavily dependent on how strict the security settings are for PHP on your server. We will discuss the methods of accessing these variables in more detail when we talk about processing user data, but using the arrays is a good coding practice. Some of the environment variables include:
We will talk about these more when we talk about processing data from the user. However, if you are not using Apache, you probably want to read the official PHP documentation and your server documentation to see if your server creates different environmental variables than Apache.
If you are using Apache, then there are as many as three ways to access the values of the environment variables. Say, for instance, that a client sends back a cookie to the server named // major shortcut $newVar = $myFirstCookie; // moderate shortcut $newVar = $_COOKIE["myFirstCookie"]; // full version $newVar = $HTTP_COOKIE_VARS["myFirstCookie"]; Currently there is a push to make the second version the new standard, since it is shorter and thus makes the code easier to read. Especially when doing a great deal of cookie or form data processing.
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
|