Home > Tutorials > PHP > Variables

Variables

Without variables, dynamic content becomes harder to create. So, let's start off with the basics of these building blocks:

Declaration/Syntax

Variables start with the $ symbol, and are declared with the simple statement $var=<expression>;
Their data type will be determined by whatever its value is.

Data Types

The following is a list of the various data types:

  • String
  • Float
  • Integer
  • Boolean

If two different data types are included in an expression, PHP will attempt to reconcile this difference. However, Strings can not be added or subtracted from the other data types; rather, a string's integer and float value is 0. The following code demonstrates: <?php $var1=1; //integer $var2=3.14; //float $var3="String"; //string $var4=true; //boolean echo $var1+$var2; //outputs "4.14" echo $var1+$var3; //outputs "1" echo $var1 . $var3; //outputs "1String" echo $var1+$var4; //outputs "2" ?> As you can see, there is only one way to concatenate strings, and that is to use the '.' operator. If any other assignment operator is used, the string will be assumed to be a number.

PHP also supports the use of arrays. There are two ways to declare an array:

  • Using the array() statement: $arr=array("Element 1", "Element 2", "Element 3") The items you pass will automatically be filled into the array in the order you list them, starting at index 0.
  • Simply referencing the array index you want to place an item in an array:$arr[3]="Array Item" Associative arrays are allowed, too, so feel free to use meaningful words as indexes.

Arrays can be multi-dimensional, as well, so it may be easiest to hold information such as demographics in one large multi-dimensional array: $demographics['Kevin']['dob']="January 31, 1972"; $demographics['Kevin']['street address']="123 Oak Street"; $demographics['Kevin']['City']="Anytown"; $demographics['Kevin']['zip']=12345; $demographics['Quailman']['dob']="July 23, 1980"; $demographics['Quailman']['street address']="21 Jumbo Street"; $demographics['Quailman']['City']="Bluffington"; $demographics['Quailman']['zip']=99987;

Scope

Variables are local and non-static in scope - $a in function a() is completely different from $a in function b(), and will be reset at the start of every function call. However, the $global and $static keywords can fix these easily enough - by specifying a variable as $global, it is accessible by all other functions which specify it as $global. Note: the variable must be declared $global in all functions in which it is used. For example, in the following piece of code: file.php <?php $global $var; function foobar() { $var=5; } function main() { $global $var=2; foobar(); echo $var; } ?> the output of main() will be 2, as $var is not declared $global in foobar.

System Variables

PHP has a list of system variables which will keep track of almost anything you could want to know:

$_SERVER
An associative array containing headers, paths, and script locations. Running through the current contents of the $_SERVER array will give the following:
Subscriptvalue
subscript 1value 1
subscript 2value 2
subscript 3value 3
$_GET
An associative array containing variables passed through the URL. Variables are separated from the URL location by the '?' symbol, and variables are separated from each other by the '&' symbol. In the following url:
www.website.com/index.php?var1=17&var2=31
the variables passed are var1 (which equals 17) and var2 (which equals 31).
$_POST
An associative array of variables passed through the HTTP POST method. These are the variables passed through forms.
$_FILES
An associative array of files uploaded to the current script.
$_COOKIE
An associative array of variables passed via HTTP COOKIES.
$_REQUEST
An associative array which contains $_GET, $_POST, and $_COOKIE.
$_SESSION
An associative array which contains session variables.
$php_errormsg
A variable containing the text of the last error message generated by PHP. This variable will only be available within the scope in which the error occurred, and only if the track_errors configuration option is turned on (it defaults to off).
$http_response_header
An associative array containing the headers sent by the server in response to a HTTP request. Running through the current contents of this array will result in something such as this:
Subscriptvalue
subscript 1value 1
subscript 2value 2
subscript 3value 3
$argc
When running from the command line, contains the number of variables passed to the script.
$argv
When running from the command line, an array of variables passed to the script.
$GLOBALS
An associative array of all global variables.

<<PreviousNext>>

Comment on this page:

Your information Name:
Email:
Hide Email?
Enter the 5 characters shown.
Request another image.
Prove your human:
Comments Overall page rating:

Comments

No one has commented on this lesson yet. You can be the first!