Basics
- Variables in PHP are represented by a dollar sign followed by the
- name of the variable. The variable name is case-sensitive.
-
-
+ Variables in PHP are represented by a dollar sign followed by the
+ name of the variable. The variable name is case-sensitive.
+
+
$var = "Bob";
$Var = "Joe";
echo "$var, $Var"; // outputs "Bob, Joe"
-
-
+
+
-
- In PHP3, variables are always assigned by value. That is to say,
- when you assign an expression to a variable, the entire value of
- the original expression is copied into the destination
- variable. This means, for instance, that after assigning one
- variable's value to another, changing one of those variables will
- have no effect on the other. For more information on this kind of
- assignment, see Expressions.
+ In PHP3, variables are always assigned by value. That is to say,
+ when you assign an expression to a variable, the entire value of
+ the original expression is copied into the destination
+ variable. This means, for instance, that after assigning one
+ variable's value to another, changing one of those variables will
+ have no effect on the other. For more information on this kind of
+ assignment, see Expressions.
-
- PHP4 offers another way to assign values to variables:
- assign by reference. This means that the new
- variable simply references (in other words, "becomes an alias for"
- or "points to") the original variable. Changes to the new variable
- affect the original, and vice versa. This also means that no
- copying is performed; thus, the assignment happens more
- quickly. However, any speedup will likely be noticed only in tight
- loops or when assigning large arrays or objects.
+ PHP4 offers another way to assign values to variables:
+ assign by reference. This means that the new
+ variable simply references (in other words, "becomes an alias for"
+ or "points to") the original variable. Changes to the new variable
+ affect the original, and vice versa. This also means that no
+ copying is performed; thus, the assignment happens more
+ quickly. However, any speedup will likely be noticed only in tight
+ loops or when assigning large arrays or objects.
-
- To assign by reference, simply prepend an ampersand (&) to the
- beginning of the variable which is being assigned (the source
- variable). For instance, the following code snippet outputs 'My
- name is Bob' twice:
+ To assign by reference, simply prepend an ampersand (&) to the
+ beginning of the variable which is being assigned (the source
+ variable). For instance, the following code snippet outputs 'My
+ name is Bob' twice:
-
-
+
+
<?php
$foo = 'Bob'; // Assign the value 'Bob' to $foo
$bar = &$foo; // Reference $foo via $bar.
@@ -53,16 +50,15 @@ $bar = "My name is $bar"; // Alter $bar...
echo $foo; // $foo is altered too.
echo $bar;
?>
-
-
-
+
+
- One important thing to note is that only named variables may be
- assigned by reference.
-
-
+ One important thing to note is that only named variables may be
+ assigned by reference.
+
+
<?php
$foo = 25;
$bar = &$foo; // This is a valid assignment.
@@ -74,8 +70,8 @@ function test() {
$bar = &test(); // Invalid.
?>
-
-
+
+
@@ -84,449 +80,445 @@ $bar = &test(); // Invalid.
Predefined variables
- PHP provides a large number of predefined variables to any script
- which it runs. Many of these variables, however, cannot be fully
- documented as they are dependent upon which server is running, the
- version and setup of the server, and other factors. Some of these
- variables will not be available when PHP is run on the
- command-line.
+ PHP provides a large number of predefined variables to any script
+ which it runs. Many of these variables, however, cannot be fully
+ documented as they are dependent upon which server is running, the
+ version and setup of the server, and other factors. Some of these
+ variables will not be available when PHP is run on the
+ command-line.
-
- Despite these factors, here is a list of predefined variables
- available under a stock installation of PHP 3 running as a module
- under a stock installation of Apache 1.3.6.
+ Despite these factors, here is a list of predefined variables
+ available under a stock installation of PHP 3 running as a module
+ under a stock installation of Apache 1.3.6.
-
- For a list of all predefined variables (and lots of other useful
- information), please see (and use) phpinfo.
+ For a list of all predefined variables (and lots of other useful
+ information), please see (and use) phpinfo.
-
- This list is neither exhaustive nor intended to be. It is simply
- a guideline as to what sorts of predefined variables you can
- expect to have access to in your script.
-
+
+ This list is neither exhaustive nor intended to be. It is simply
+ a guideline as to what sorts of predefined variables you can
+ expect to have access to in your script.
+
- Apache variables
+ Apache variables
+
+ These variables are created by the Apache webserver. If you are running
+ another webserver, there is no guarantee that it will provide the
+ same variables; it may omit some, or provide others not listed
+ here. That said, a large number of these variables are accounted
+ for in the CGI 1.1
+ specification, so you should be able to expect those.
+
+
+ Note that few, if any, of these will be available (or indeed have
+ any meaning) if running PHP on the command line.
+
+
+
+
+
+ GATEWAY_INTERFACE
+
- These variables are created by the Apache webserver. If you are running
- another webserver, there is no guarantee that it will provide the
- same variables; it may omit some, or provide others not listed
- here. That said, a large number of these variables are accounted
- for in the CGI 1.1
- specification, so you should be able to expect those.
+ What revision of the CGI specification the server is using;
+ i.e. 'CGI/1.1'.
+
+
+
+ SERVER_NAME
+
- Note that few, if any, of these will be available (or indeed have
- any meaning) if running PHP on the command line.
+ The name of the server host under which the current script is
+ executing. If the script is running on a virtual host, this
+ will be the value defined for that virtual host.
+
+
-
-
-
- GATEWAY_INTERFACE
-
-
- What revision of the CGI specification the server is using;
- i.e. 'CGI/1.1'.
-
-
-
+
+ SERVER_SOFTWARE
+
+
+ Server identification string, given in the headers when
+ responding to requests.
+
+
+
-
- SERVER_NAME
-
-
- The name of the server host under which the current script is
- executing. If the script is running on a virtual host, this
- will be the value defined for that virtual host.
-
-
-
-
-
- SERVER_SOFTWARE
-
-
- Server identification string, given in the headers when
- responding to requests.
-
-
-
-
-
- SERVER_PROTOCOL
-
-
- Name and revision of the information protocol via which the
- page was requested; i.e. 'HTTP/1.0';
-
-
-
-
-
- REQUEST_METHOD
-
-
- Which request method was used to access the page; i.e. 'GET',
- 'HEAD', 'POST', 'PUT'.
-
-
-
+
+ SERVER_PROTOCOL
+
+
+ Name and revision of the information protocol via which the
+ page was requested; i.e. 'HTTP/1.0';
+
+
+
+
+
+ REQUEST_METHOD
+
+
+ Which request method was used to access the page; i.e. 'GET',
+ 'HEAD', 'POST', 'PUT'.
+
+
+
-
- QUERY_STRING
-
-
- The query string, if any, via which the page was accessed.
-
-
-
+
+ QUERY_STRING
+
+
+ The query string, if any, via which the page was accessed.
+
+
+
-
- DOCUMENT_ROOT
-
-
- The document root directory under which the current script is
- executing, as defined in the server's configuration file.
-
-
-
+
+ DOCUMENT_ROOT
+
+
+ The document root directory under which the current script is
+ executing, as defined in the server's configuration file.
+
+
+
-
- HTTP_ACCEPT
-
-
- Contents of the Accept: header
- from the current request, if there is one.
-
-
-
+
+ HTTP_ACCEPT
+
+
+ Contents of the Accept: header from the
+ current request, if there is one.
+
+
+
-
- HTTP_ACCEPT_CHARSET
-
-
- Contents of the Accept-Charset: header
- from the current request, if there is one. Example:
- 'iso-8859-1,*,utf-8'.
-
-
-
+
+ HTTP_ACCEPT_CHARSET
+
+
+ Contents of the Accept-Charset: header
+ from the current request, if there is one. Example:
+ 'iso-8859-1,*,utf-8'.
+
+
+
-
- HTTP_ENCODING
-
-
- Contents of the Accept-Encoding: header
- from the current request, if there is one. Example: 'gzip'.
-
-
-
+
+ HTTP_ENCODING
+
+
+ Contents of the Accept-Encoding: header
+ from the current request, if there is one. Example: 'gzip'.
+
+
+
+
+
+ HTTP_ACCEPT_LANGUAGE
+
+
+ Contents of the Accept-Language: header
+ from the current request, if there is one. Example: 'en'.
+
+
+
+
+
+ HTTP_CONNECTION
+
+
+ Contents of the Connection: header from
+ the current request, if there is one. Example: 'Keep-Alive'.
+
+
+
-
- HTTP_ACCEPT_LANGUAGE
-
-
- Contents of the Accept-Language: header
- from the current request, if there is one. Example: 'en'.
-
-
-
+
+ HTTP_HOST
+
+
+ Contents of the Host: header from the
+ current request, if there is one.
+
+
+
-
- HTTP_CONNECTION
-
-
- Contents of the Connection: header
- from the current request, if there is one. Example:
- 'Keep-Alive'.
-
-
-
+
+ HTTP_REFERER
+
+
+ The address of the page (if any) which referred the browser
+ to the current page. This is set by the user's browser; not
+ all browsers will set this.
+
+
+
-
- HTTP_HOST
-
-
- Contents of the Host: header
- from the current request, if there is one.
-
-
-
+
+ HTTP_USER_AGENT
+
+
+ Contents of the User_Agent: header from
+ the current request, if there is one. This is a string
+ denoting the browser software being used to view the current
+ page; i.e. Mozilla/4.5 [en] (X11; U; Linux
+ 2.2.9 i586). Among other things, you can use
+ this value with get_browser to tailor
+ your page's functionality to the capabilities of the user's
+ browser.
+
+
+
-
- HTTP_REFERER
-
-
- The address of the page (if any) which referred the browser
- to the current page. This is set by the user's browser; not
- all browsers will set this.
-
-
-
+
+ REMOTE_ADDR
+
+
+ The IP address from which the user is viewing the current
+ page.
+
+
+
-
- HTTP_USER_AGENT
-
-
- Contents of the User_Agent: header from
- the current request, if there is one. This is a string
- denoting the browser software being used to view the current
- page; i.e. Mozilla/4.5 [en] (X11; U; Linux
- 2.2.9 i586). Among other things, you can use
- this value with get_browser to tailor
- your page's functionality to the capabilities of the user's
- browser.
-
-
-
+
+ REMOTE_PORT
+
+
+ The port being used on the user's machine to communicate with
+ the web server.
+
+
+
-
- REMOTE_ADDR
-
-
- The IP address from which the user is viewing the current
- page.
-
-
-
+
+ SCRIPT_FILENAME
+
+
+ The absolute pathname of the currently executing script.
+
+
+
-
- REMOTE_PORT
-
-
- The port being used on the user's machine to communicate with
- the web server.
-
-
-
+
+ SERVER_ADMIN
+
+
+ The value given to the SERVER_ADMIN (for Apache) directive in
+ the web server configuration file. If the script is running
+ on a virtual host, this will be the value defined for that
+ virtual host.
+
+
+
+
+
+ SERVER_PORT
+
+
+ The port on the server machine being used by the web server
+ for communication. For default setups, this will be '80';
+ using SSL, for instance, will change this to whatever your
+ defined secure HTTP port is.
+
+
+
-
- SCRIPT_FILENAME
-
-
- The absolute pathname of the currently executing script.
-
-
-
+
+ SERVER_SIGNATURE
+
+
+ String containing the server version and virtual host name
+ which are added to server-generated pages, if enabled.
+
+
+
-
- SERVER_ADMIN
-
-
- The value given to the SERVER_ADMIN (for Apache) directive in
- the web server configuration file. If the script is running
- on a virtual host, this will be the value defined for that
- virtual host.
-
-
-
+
+ PATH_TRANSLATED
+
+
+ Filesystem- (not document root-) based path to the current
+ script, after the server has done any virtual-to-real
+ mapping.
+
+
+
-
- SERVER_PORT
-
-
- The port on the server machine being used by the web server
- for communication. For default setups, this will be '80';
- using SSL, for instance, will change this to whatever your
- defined secure HTTP port is.
-
-
-
+
+ SCRIPT_NAME
+
+
+ Contains the current script's path. This is useful for pages
+ which need to point to themselves.
+
+
+
-
- SERVER_SIGNATURE
-
-
- String containing the server version and virtual host name
- which are added to server-generated pages, if enabled.
-
-
-
+
+ REQUEST_URI
+
+
+ The URI which was given in order to access this page; for
+ instance, '/index.html'.
+
+
+
+
+
-
- PATH_TRANSLATED
-
-
- Filesystem- (not document root-) based path to the current
- script, after the server has done any virtual-to-real
- mapping.
-
-
-
-
-
- SCRIPT_NAME
-
-
- Contains the current script's path. This is useful for pages
- which need to point to themselves.
-
-
-
-
-
- REQUEST_URI
-
-
- The URI which was given in order to access this page; for
- instance, '/index.html'.
-
-
-
-
-
-
- Environment variables
+ Environment variables
-
- These variables are imported into PHP's global namespace from the
- environment under which the PHP parser is running. Many are
- provided by the shell under which PHP is running and different
- systems are likely running different kinds of shells, a
- definitive list is impossible. Please see your shell's
- documentation for a list of defined environment variables.
-
-
-
- Other environment variables include the CGI variables, placed
- there regardless of whether PHP is running as a server module or
- CGI processor.
-
+
+ These variables are imported into PHP's global namespace from the
+ environment under which the PHP parser is running. Many are
+ provided by the shell under which PHP is running and different
+ systems are likely running different kinds of shells, a
+ definitive list is impossible. Please see your shell's
+ documentation for a list of defined environment variables.
+
+
+ Other environment variables include the CGI variables, placed
+ there regardless of whether PHP is running as a server module or
+ CGI processor.
+
- PHP variables
+ PHP variables
+
+ These variables are created by PHP itself.
+
+
+
+
+
+ argv
+
- These variables are created by PHP itself.
+ Array of arguments passed to the script. When the script is
+ run on the command line, this gives C-style access to the
+ command line parameters. When called via the GET method, this
+ will contain the query string.
+
+
-
-
-
- argv
-
-
- Array of arguments passed to the script. When the script is
- run on the command line, this gives C-style access to the
- command line parameters. When called via the GET method, this
- will contain the query string.
-
-
-
+
+ argc
+
+
+ Contains the number of command line parameters passed to the
+ script (if run on the command line).
+
+
+
-
- argc
-
-
- Contains the number of command line parameters passed to the
- script (if run on the command line).
-
-
-
-
-
- PHP_SELF
-
-
- The filename of the currently executing script, relative to
- the document root. If PHP is running as a command-line
- processor, this variable is not available.
-
-
-
+
+ PHP_SELF
+
+
+ The filename of the currently executing script, relative to
+ the document root. If PHP is running as a command-line
+ processor, this variable is not available.
+
+
+
-
- HTTP_COOKIE_VARS
-
-
- An associative array of variables passed to the current
- script via HTTP cookies. Only available if variable
- tracking has been turned on via either the track_vars configuration
- directive or the
- <?php_track_vars?>
- directive.
-
-
-
+
+ HTTP_COOKIE_VARS
+
+
+ An associative array of variables passed to the current
+ script via HTTP cookies. Only available if variable tracking
+ has been turned on via either the track_vars configuration
+ directive or the
+ <?php_track_vars?>
+ directive.
+
+
+
+
+
+ HTTP_GET_VARS
+
+
+ An associative array of variables passed to the current
+ script via the HTTP GET method. Only available if variable
+ tracking has been turned on via either the track_vars configuration
+ directive or the
+ <?php_track_vars?>
+ directive.
+
+
+
-
- HTTP_GET_VARS
-
-
- An associative array of variables passed to the current
- script via the HTTP GET method. Only available if variable
- tracking has been turned on via either the track_vars configuration
- directive or the
- <?php_track_vars?>
- directive.
-
-
-
-
-
- HTTP_POST_VARS
-
-
- An associative array of variables passed to the current
- script via the HTTP POST method. Only available if variable
- tracking has been turned on via either the track_vars configuration
- directive or the
- <?php_track_vars?>
- directive.
-
-
-
-
-
-
+
+ HTTP_POST_VARS
+
+
+ An associative array of variables passed to the current
+ script via the HTTP POST method. Only available if variable
+ tracking has been turned on via either the track_vars configuration
+ directive or the
+ <?php_track_vars?>
+ directive.
+
+
+
+
+
-
- Variable scope
+
+ Variable scope
-
- The scope of a variable is the context within which it is defined.
- For the most part all PHP variables only have a single scope. This
- single scope spans included and required files as well. For example:
-
-
-
+
+ The scope of a variable is the context within which it is defined.
+ For the most part all PHP variables only have a single scope.
+ This single scope spans included and required files as well. For
+ example:
+
+
+
$a = 1;
include "b.inc";
-
-
-
- Here the $a variable will be available within the included b.inc
- script.
- However, within user-defined functions a local function scope is
- introduced. Any variable used inside a function is by default
- limited to the local function scope. For example:
+
+
+
+ Here the $a variable will be available within the included b.inc
+ script. However, within user-defined functions a local function
+ scope is introduced. Any variable used inside a function is by
+ default limited to the local function scope. For
+ example:
+
-
-
+
+
$a = 1; /* global scope */
Function Test () {
@@ -534,22 +526,22 @@ Function Test () {
}
Test ();
-
-
+
+
-
- This script will not produce any output because the echo statement
- refers to a local version of the $a variable, and it has not been
- assigned a value within this scope. You may notice that this is a
- little bit different from the C language in that global variables
- in C are automatically available to functions unless specifically
- overridden by a local definition. This can cause some problems in
- that people may inadvertently change a global variable. In PHP
- global variables must be declared global inside a function if they
- are going to be used in that function. An example:
+
+ This script will not produce any output because the echo statement
+ refers to a local version of the $a variable, and it has not been
+ assigned a value within this scope. You may notice that this is a
+ little bit different from the C language in that global variables
+ in C are automatically available to functions unless specifically
+ overridden by a local definition. This can cause some problems in
+ that people may inadvertently change a global variable. In PHP
+ global variables must be declared global inside a function if they
+ are going to be used in that function. An example:
-
-
+
+
$a = 1;
$b = 2;
@@ -561,22 +553,25 @@ Function Sum () {
Sum ();
echo $b;
-
-
+
+
-
- The above script will output "3". By declaring $a and
- $b global within the function, all references to either variable
- will refer to the global version. There is no limit to the number
- of global variables that can be manipulated by a function.
+
+ The above script will output "3". By declaring $a and
+ $b global within the function, all references to either variable
+ will refer to the global version. There is no limit to the number
+ of global variables that can be manipulated by a
+ function.
+
-
- A second way to access variables from the global scope is to use
- the special PHP-defined $GLOBALS array. The previous example can
- be rewritten as:
+
+ A second way to access variables from the global scope is to use
+ the special PHP-defined $GLOBALS array. The previous example can
+ be rewritten as:
+
-
-
+
+
$a = 1;
$b = 2;
@@ -586,65 +581,68 @@ Function Sum () {
Sum ();
echo $b;
-
-
+
+
-
- The $GLOBALS array is an associative array with the name of the
- global variable being the key and the contents of that variable
- being the value of the array element.
+
+ The $GLOBALS array is an associative array with the name of the
+ global variable being the key and the contents of that variable
+ being the value of the array element.
+
-
- Another important feature of variable scoping is the
- static variable. A static variable exists
- only in a local function scope, but it does not lose its value
- when program execution leaves this scope. Consider the following
- example:
+
+ Another important feature of variable scoping is the
+ static variable. A static variable exists
+ only in a local function scope, but it does not lose its value
+ when program execution leaves this scope. Consider the following
+ example:
+
-
-
+
+
Function Test () {
$a = 0;
echo $a;
$a++;
}
-
-
+
+
-
- This function is quite useless since every time it is called it
- sets $a to 0 and prints "0". The $a++ which increments
- the variable serves no purpose since as soon as the function exits
- the $a variable disappears. To make a useful counting function
- which will not lose track of the current count, the $a variable is
- declared static:
+
+ This function is quite useless since every time it is called it
+ sets $a to 0 and prints "0". The $a++ which increments
+ the variable serves no purpose since as soon as the function exits
+ the $a variable disappears. To make a useful counting function
+ which will not lose track of the current count, the $a variable is
+ declared static:
-
-
+
+
Function Test () {
static $a = 0;
echo $a;
$a++;
}
-
-
-
-
- Now, every time the Test() function is called it will print the
- value of $a and increment it.
+
+
- Static variables also provide one way to deal with recursive
- functions. A recursive function is one which calls itself. Care
- must be taken when writing a recursive function because it is
- possible to make it recurse indefinitely. You must make sure you
- have an adequate way of terminating the recursion. The following
- simple function recursively counts to 10, using the static
- variable $count to know when to stop:
+ Now, every time the Test() function is called it will print the
+ value of $a and increment it.
-
-
+
+ Static variables also provide one way to deal with recursive
+ functions. A recursive function is one which calls itself. Care
+ must be taken when writing a recursive function because it is
+ possible to make it recurse indefinitely. You must make sure you
+ have an adequate way of terminating the recursion. The following
+ simple function recursively counts to 10, using the static
+ variable $count to know when to stop:
+
+
+
+
Function Test () {
static $count = 0;
@@ -655,108 +653,118 @@ Function Test () {
}
$count--;
}
-
-
+
+
-
- Variable variables
+
-
- Sometimes it is convenient to be able to have variable variable
- names. That is, a variable name which can be set and used
- dynamically. A normal variable is set with a statement such as:
+
+ Variable variables
+
+
+ Sometimes it is convenient to be able to have variable variable
+ names. That is, a variable name which can be set and used
+ dynamically. A normal variable is set with a statement such as:
-
-
+
+
$a = "hello";
-
-
+
+
-
- A variable variable takes the value of a variable and treats that as the
- name of a variable. In the above example, hello, can
- be used as the name of a variable by using two dollar signs. i.e.
+
+ A variable variable takes the value of a variable and treats that
+ as the name of a variable. In the above example,
+ hello, can be used as the name of a variable
+ by using two dollar signs. i.e.
-
-
+
+
$$a = "world";
-
-
+
+
-
- At this point two variables have been defined and stored in the
- PHP symbol tree: $a with contents "hello" and $hello with contents
- "world". Therefore, this statement:
+
+ At this point two variables have been defined and stored in the
+ PHP symbol tree: $a with contents "hello" and $hello with contents
+ "world". Therefore, this statement:
-
-
+
+
echo "$a ${$a}";
-
-
+
+
-
- produces the exact same output as:
+
+ produces the exact same output as:
-
-
+
+
echo "$a $hello";
-
-
+
+
-
+
i.e. they both produce: hello world.
+
+ In order to use variable variables with arrays, you have to
+ resolve an ambiguity problem. That is, if you write $$a[1] then
+ the parser needs to know if you meant to use $a[1] as a variable,
+ or if you wanted $$a as the variable and then the [1] index from
+ that variable. The syntax for resolving this ambiguity is:
+ ${$a[1]} for the first case and ${$a}[1] for the
+ second.
+
+
+
+
+
+ Variables from outside PHP
+
+
+ HTML Forms (GET and POST)
+
- In order to use variable variables with arrays, you have to
- resolve an ambiguity problem. That is, if you write $$a[1] then
- the parser needs to know if you meant to use $a[1] as a variable,
- or if you wanted $$a as the variable and then the [1] index from
- that variable. The syntax for resolving this ambiguity is:
- ${$a[1]} for the first case and ${$a}[1] for the second.
+ When a form is submitted to a PHP script, any variables from that
+ form will be automatically made available to the script by
+ PHP. For instance, consider the following form:
+
-
- Variables from outside PHP
-
-
- HTML Forms (GET and POST)
-
-
- When a form is submitted to a PHP script, any variables from that
- form will be automatically made available to the script by
- PHP. For instance, consider the following form:
-
-
-
- Simple form variable
-
+
+
+ Simple form variable
+
<form action="foo.php3" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>
-
-
+
+
+
-
- When submitted, PHP will create the variable
- $name, which will will contain
- whatever what entered into the Name: field
- on the form.
+
+ When submitted, PHP will create the variable
+ $name, which will will contain
+ whatever what entered into the Name: field
+ on the form.
+
-
- PHP also understands arrays in the context of form variables, but
- only in one dimension. You may, for example, group related
- variables together, or use this feature to retrieve values from a
- multiple select input:
+
+ PHP also understands arrays in the context of form variables, but
+ only in one dimension. You may, for example, group related
+ variables together, or use this feature to retrieve values from a
+ multiple select input:
-
+ More complex form variables
-
+
<form action="array.php" method="post">
Name: <input type="text" name="personal[name]"><br>
Email: <input type="text" name="personal[email]"><br>
@@ -764,144 +772,163 @@ echo "$a $hello";
<select multiple name="beer[]">
<option value="warthog">Warthog
<option value="guinness">Guinness
+ <option value="stuttgarter">Stuttgarter Schwabenbräu
</select>
<input type="submit">
</form>
-
-
+
+
+
-
- If PHP's track_vars feature is turned on, either by the track_vars configuration setting
- or the <?php_track_vars?>
- directive, then variables submitted via the POST or GET methods
- will also be found in the global associative arrays
- $HTTP_POST_VARS and $HTTP_GET_VARS as appropriate.
+
+ If PHP's track_vars feature is turned on, either by the track_vars configuration setting
+ or the <?php_track_vars?>
+ directive, then variables submitted via the POST or GET methods
+ will also be found in the global associative arrays
+ $HTTP_POST_VARS and $HTTP_GET_VARS as appropriate.
+
-
- IMAGE SUBMIT variable names
-
-
- When submitting a form, it is possible to use an image instead
- of the standard submit button with a tag like:
-
-
-
-<input type=image src="image.gif" name="sub">
-
-
-
-
- When the user clicks somewhere on the image, the accompanying
- form will be transmitted to the server with two additional
- variables, sub_x and sub_y. These contain the coordinates of
- the user click within the image. The experienced may note that
- the actual variable names sent by the browser contains a period
- rather than an underscore, but PHP converts the period to an
- underscore automatically.
-
-
- HTTP Cookies
+
+ IMAGE SUBMIT variable names
- PHP transparently supports HTTP cookies as defined by Netscape's Spec. Cookies are a
- mechanism for storing data in the remote browser and thus
- tracking or identifying return users. You can set cookies using
- the SetCookie function. Cookies are part
- of the HTTP header, so the SetCookie function must be called
- before any output is sent to the browser. This is the same
- restriction as for the Header function.
- Any cookies sent to you from the client will automatically be
- turned into a PHP variable just like GET and POST method data.
-
-
- If you wish to assign multiple values to a single cookie, just
- add [] to the cookie name. For example:
+ When submitting a form, it is possible to use an image instead
+ of the standard submit button with a tag like:
-
-SetCookie ("MyCookie[]", "Testing", time()+3600);
+
+<input type=image src="image.gif" name="sub">
- Note that a cookie will replace a previous cookie by the same
- name in your browser unless the path or domain is different. So,
- for a shopping cart application you may want to keep a counter
- and pass this along. i.e.
+ When the user clicks somewhere on the image, the accompanying
+ form will be transmitted to the server with two additional
+ variables, sub_x and sub_y. These contain the coordinates of the
+ user click within the image. The experienced may note that the
+ actual variable names sent by the browser contains a period
+ rather than an underscore, but PHP converts the period to an
+ underscore automatically.
+
+
-
- SetCookie Example
-
+
+
+
+ HTTP Cookies
+
+
+ PHP transparently supports HTTP cookies as defined by Netscape's Spec. Cookies are a
+ mechanism for storing data in the remote browser and thus
+ tracking or identifying return users. You can set cookies using
+ the SetCookie function. Cookies are part of
+ the HTTP header, so the SetCookie function must be called before
+ any output is sent to the browser. This is the same restriction
+ as for the Header function. Any cookies
+ sent to you from the client will automatically be turned into a
+ PHP variable just like GET and POST method data.
+
+
+ If you wish to assign multiple values to a single cookie, just
+ add [] to the cookie name. For
+ example:
+
+
+
+
+SetCookie ("MyCookie[]", "Testing", time()+3600);
+
+
+
+
+ Note that a cookie will replace a previous cookie by the same
+ name in your browser unless the path or domain is different. So,
+ for a shopping cart application you may want to keep a counter
+ and pass this along. i.e.
+
+
+
+ SetCookie Example
+
$Count++;
SetCookie ("Count", $Count, time()+3600);
SetCookie ("Cart[$Count]", $item, time()+3600);
-
-
+
+
-
- Environment variables
+
-
- PHP automatically makes environment variables available as normal
- PHP variables.
+
+ Environment variables
+
+
+ PHP automatically makes environment variables available as normal
+ PHP variables.
-
+
echo $HOME; /* Shows the HOME environment variable, if set. */
-
+
+
-
- Since information coming in via GET, POST and Cookie mechanisms
- also automatically create PHP variables, it is sometimes best to
- explicitly read a variable from the environment in order to make
- sure that you are getting the right version. The
- getenv function can be used for this. You
- can also set an environment variable with the
- putenv function.
-
-
- Dots in incoming variable names
-
-
- Typically, PHP does not alter the names of variables when they are
- passed into a script. However, it should be noted that the dot
- (period, full stop) is not a valid character in a PHP
- variable name. For the reason, look at it:
-
-$varname.ext; /* invalid variable name */
-
- Now, what the parser sees is a variable named $varname, followed
- by the string concatenation operator, followed by the barestring
- (i.e. unquoted string which doesn't match any known key or
- reserved words) 'ext'. Obviously, this doesn't have the intended
- result.
-
-
-
- For this reason, it is important to note that PHP will
- automatically replace any dots in incoming variable names with
- underscores.
-
-
-
-
- Determining variable types
-
-
- Because PHP determines the types of variables and converts them
- (generally) as needed, it is not always obvious what type a given
- variable is at any one time. PHP includes several functions
- which find out what type a variable is. They are
- gettype, is_long,
- is_double, is_string,
- is_array, and
- is_object.
-
+
+ Since information coming in via GET, POST and Cookie mechanisms
+ also automatically create PHP variables, it is sometimes best to
+ explicitly read a variable from the environment in order to make
+ sure that you are getting the right version. The
+ getenv function can be used for this. You
+ can also set an environment variable with the
+ putenv function.
+
+
+
+ Dots in incoming variable names
+
+
+ Typically, PHP does not alter the names of variables when they
+ are passed into a script. However, it should be noted that the
+ dot (period, full stop) is not a valid character in a PHP
+ variable name. For the reason, look at it:
+
+$varname.ext; /* invalid variable name */
+
+ Now, what the parser sees is a variable named $varname, followed
+ by the string concatenation operator, followed by the barestring
+ (i.e. unquoted string which doesn't match any known key or
+ reserved words) 'ext'. Obviously, this doesn't have the intended
+ result.
+
+
+
+ For this reason, it is important to note that PHP will
+ automatically replace any dots in incoming variable names with
+ underscores.
+
+
+
+
+
+ Determining variable types
+
+
+ Because PHP determines the types of variables and converts them
+ (generally) as needed, it is not always obvious what type a given
+ variable is at any one time. PHP includes several functions
+ which find out what type a variable is. They are
+ gettype, is_long,
+ is_double, is_string,
+ is_array, and
+ is_object.
+
+
+
-
+