Learn web design and programming with our free video and text tutorials.
Web Designer? Design and market your own professional website with easy-to-use tools.
A variable is used to store values like strings, for example the text "Hello World!", and numbers, for example the number 10. Once a variable is declared, it can be used over and over again throughout your script.
To declare a variable in PHP, first type the dollar sign symbol ($), followed by the name of the variable. The name of the variable is followed by the assignment operator (=), which is followed by the value that you want to assign to the variable, followed by the semi-colon (;).
Variable names can contain any combination of letters, numbers and the underscore symbol (_). A variable must begin with either a letter or the underscore symbol and can not contain any blank spaces.
As a rule, variable names that begin with a letter should begin with a lower-case letter and each word that follows the first word within that variable should either begin with a capital letter or be preceded by an underscore. For example, $someText or $some_text.
An example of the syntax of PHP variables is below.
<?php
$variable_name = value;
?>
PHP is a loosely type language, which means that you do not need to declare a variable before assigning a value to it.
The PHP code below shows you how to create a numeric variable and output it onto the web browser.
<?php
$some_number = 10;
echo $some_number;
?>
The PHP code above outputs the text below.
10
The PHP code below shows you how to create a string variable and output it onto the web browser.
<?php
$some_text = "Hello World!";
echo $some_text;
?>
The PHP code above outputs the text below.
Hello World!
variable type | explanation |
---|---|
integer | whole number |
double | real number |
string | a string of characters |
boolean | true of false |
array | list of items |
object | instance of a class |
Every variable that is defined in a PHP script within an area of accessibility called a scope. The scope of that variable determines which elements in that script can access that variable.
PHP variables are either superglobal, global or local with superglobal and global being the two most common type of scopes.
All variables that are defined within the main body of a script are global variables. Variables that are defined in the body of a function are local for the function. Local variables are not accessible in the global scope. This scope includes included and required files also.
Superglobal variables are built-in arrays that have a global scope. Superglobal arrays are available anywhere in your program, including in functions.
The table below lists the most widely used superglobal arrays.
Array | Description |
---|---|
$_POST | Contains all of the variables that are submitted in a form using the POST method. |
$_GET | Contains all of the variables that are submitted in a form using the GET method. |
$_COOKIE | Contains all of the cookie variables. |
$_REQUEST | Contains all of the variables that are in the $_POST, $_GET and $_COOKIE. |
$_FILES | Contains the names of files that have been uploaded. |
$_SERVER | Contains information about your server. |
$_ENV | Contains information that is provided by your operating system. |
$GLOBALS | Contains all of the global variables. |
Variables that are defined in the main body of a script are global variables. Variables that are defined in the body of a function are local to the function.
Global variables are not accessible in the local scope and local variables are not accessible in the global scope unless they are re-defined within the local scope.
To show you the difference between local and global variables, look and the PHP code below and what it outputs.
<?php
$globalVar = 1;
function localFunc() {
echo "The value in localFunc is: $globalVar.<br />";
}
function globalFunc() {
global $globalVar;
echo "The value in globalFunc is: $globalVar";
}
localFunc();
globalFunc();
?>
The PHP code above outputs:
The value in localFunc is: .
The value in globalFunc is: 1
When the localFunc() function executes, the value is not found because the variable does not exist within the function, in other words it does not exist within the local scope.
When the globalFunc() function executes, the value is output when the globalFunc() function is called because the global keyword is used to declare the variable as global.
A PHP constant is a variable that cannot be changed after the initial value is assigned during the execution of the script, except for magic constants, which are not really constants.
You create a constant variable using the define () function. The function accepts 2 arguments. The first must be a string and represents the name which will be used to access the constant. A constant name is case-sensitive by default and by convention, are always uppercase. A valid constant name starts with a letter or underscore, followed by any combination of letters, numbers or underscores, however, by convention only capital letters and underscore symbols are used. Constant names must be in quotes when they are defined.
The second argument is the value of the constant and can be a string or numbers and can be set explicitly or as as the result of a function or equation.
The scope of a constant is global, which means that you can access constants anywhere in your script without regard to scope.
PHP has many pre-defined constants that can be used in your scripts.
<?php
define ("WEBSITE", "WebDevelopmentTutorials.com");
echo WEBSITE;
?>
Magic constants, as they are called, are not acutally constants but effectively behave like constants and are pre-defined. Magic constants use the same naming convention as PHP constants.
The full list of magic constants can be found here.
There are 7 magic constants that change depending on where they are used. They are listed below.
name | description |
---|---|
__LINE__ | The current line number of the file. |
__FILE__ | The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances. |
__DIR__ | The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.) |
__FUNCTION__ | The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. |
__CLASS__ | The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. |
__METHOD__ | The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive). |
__NAMESPACE__ | The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0). |
This tutorial was last updated: 11-24-09