1. PHP Variables Basics
1.1. What are PHP Variables?
PHP variables are used to store data similar to other programming languages. Usually, the PHP variables store data in the form of numbers, text, etc. In short, they are similar to boxes, to store data in it. Additionally, below are some essential characteristics of PHP variables, that need to be understood.
- Firstly, PHP variables begin with a dollar sign $ followed by a name
- Secondly, there is no need to declare the type of a variable in PHP, before storing data in it
- Hence, PHP is a loosely typed language
- Thus, PHP automatically declares a data type of a variable
- Importantly, the assignment operator = helps in assigning the data to the variable
- Moreover, the variable name must start with a letter or an underscore (_)
- Also, a number cannot be at the start of a variable name
- Therefore, a variable name can only start with letters and underscore only
- Furthermore, PHP variables must contain numbers, letters and underscores only (A-z, 0-9 and _)
- Most importantly, variables are case sensitive, means $name and $Name are different
- Also, no spaces are allowed in variable names
- Further, 08 data types can be assigned to a variable
- Lastly, a PHP variable name can be of single character length $x to undefined length
2. How to Declare/Define a Variable in PHP?
Variables are declared in PHP with the help of an assignment operator =. See the example below to understand how to define or declare or create a PHP variable?
Example
<?php /* Declaring PHP Variables */ $name = 'John'; $age = 40; echo $name .' is '.$age.'years old.'; ?>
In the above example, the two PHP variables $name and $age are declared. Further, note that the $name variable is a string, whereas, $age variable is a number.
2.1. Valid and InValid Variable Declaration
Variable Declarations
<?php /* Valid and InValid PHP Variable Declaration */ $name = 'John'; /*valid*/ $Name = "John"; /*valid*/ $_country = 'United States'; /*valid*/ $_1age = 40; /*valid*/ $1age = 40; /*Invalid*/ $age$ = 30; /*Invalid*/ $age+country = 30; /*Invalid*/ ?>
Note:
2.2. PHP Variables Output
There are several ways to output the data of a PHP variable. Consider the following example to comprehend this vividly.
Example
<?php /* PHP Variable Output */ $tutorial = 'PHP Variable Tutroial'; $a = 100; $b = 200; echo "1-I am learning $tutorial"; echo '2-I am learning $tutorial'; echo '3-I am learning '. $tutorial; echo '4-'.($a + $b); ?>
In the above example, the PHP echo statement is used to display the variables data. Also, the first, third, and fourth statements showed variable value accurately.
- The first statement uses variable inside double quotes and displays the output as needed.
- The second statement uses a variable within single quotes and displays the variable name rather than variable value.
- The third statement comprises the variable outside the quotes with a dot (.) in between the string and variable. This is called a PHP concatenation operator that is used to join two strings.
- Ultimately, the fourth statement added the values of two variables simply.
2.3. Predefined PHP Variables
Also, there are many predefined PHP variables known as reserved variables. Below is a list of Predefined PHP variables:
$GLOBALS | References all variables available in the global scope |
$_SERVER | Server and execution environment information |
$_GET | HTTP GET variables |
$_POST | HTTP POST variables |
$_FILES | HTTP File Upload variables |
$_REQUEST | HTTP Request variables |
$_SESSION | Session variables |
$_ENV | Environment variables |
$_COOKIE | HTTP Cookies |
$php_errormsg | The previous error message |
$HTTP_RAW_POST_DATA | Raw POST data |
$http_response_header | HTTP response headers |
$argc | The number of arguments passed to script |
$argv | The array of arguments passed to script |
3. PHP Variables Scope
Most importantly, a variable scope determines the limits of a PHP variable, where it can be utilized. Precisely, a variable scope is a domain where a variable is defined. Importantly, all variables have a single scope in PHP. There are three distinct PHP variable scopes.
- Local PHP Variable Scope
- Global PHP Variable Scope
- Static PHP Variable Scope
3.1. Local PHP Variable Scope
Local variable scope implies any variable that is defined within the curly braces of a function. Hence, this variable is exclusively available to that specific function. Analyze the below example to understand local PHP variable scope:
Local Variable Example
<?php /* A local PHP variable is defined in a function and utilized */ function pie(){ $country = 'Canada'; /* local scope variable */ echo "John lives in $country."; } pie(); echo "Miller also lives in $country."; ?>
Importantly, the second statement does not display the variable value as the variable has a local scope.
3.2. Global PHP Variable Scope
Whenever, a PHP variable is defined outside of a function, it has a global variable scope. Thus, that variable is not available within the function. However, in the C++ language, this global variable is also available within the function.
Global Variable Example
<?php /* A global PHP variable is defined in a function and utilized */ $age = 20; /* global scope variable */ function pie(){ echo "My age is $age."; } pie(); ?>
In the above example, the variable is declared globally and accessed locally. Hence, the variable will not display its value.
Access Global Variable As Local Variable
However, certain methods permit the user to access the globally defined variable within the function. These include the global modifier and predefined $GLOBALS array.
a. The global Modifier
Firstly, PHP allows the use of a global modifier to access a global scope variable within a function. Hence, any number of global scope variables are easily accessed within a function by using this technique.
global Modifier Example
<?php $x1 = 20; /* global scope variable */ $x2 = 30; /* Another global variable */ $y; function addition(){ global $x1, $x2, $y; $y = $x1 + $x2; echo "The Addition of $x1 and $x2 is equal to $y."; } addition(); ?>
b. The Predefined $GLOBALS Array
Secondly, PHP has a predefined $GLOBALS array to store all the global variables. It has a syntax as $GLOBALS[index]. The index is the name of variable and is used to access a global variable within a function. It is because $GLOBALS is a superglobal variable. Let's look at the following example to understand.
$GLOBALS Array Example
<?php $x1 = 20; /* global scope variable */ $x2 = 30; /* Another global variable */ $y; function addition(){ $GLOBALS['y'] = $GLOBALS['x1'] + $GLOBALS['x2']; echo "The sum is " . $GLOBALS['y'] . "."; } addition(); ?>
3.3. Static PHP Variable Scope
Furthermore, PHP has a third type of variable named as static scope variable. They use a static modifier to make a variable static and are only available within a local function. They keep the variables alive, as normally variables are destroyed after a function is executed. Let's look at the following example to understand:
Example 1
<?php /* A static scope variale is defined and utilised. */ function johnDeposit(){ //static keyword is used in this example static $a = 0; // John deposits 100$ in his account every month $a = $a + 100; echo "John's deposits are $a$.\n"; } johnDeposit(); johnDeposit(); ?>
Example 2
<?php /* A static scope variale is defined and utilised. */ function johnDeposit(){ //static keyword is not used in this example $a = 0; // John deposits 100$ in his account every month $a = $a + 100; echo "John's deposits are $a$.\n"; } johnDeposit(); johnDeposit(); ?>
Now, the above two examples explain the importance of a static modifier. In the first example, the static modifier changed the local scope variable to a static scope variable. Hence, each time the function is called, the variable retained its new value. Whereas, in the second example, there is no static keyword. Thus, the variable $a displayed the same value each time the function is called.
4. Variable Variable
Sometimes, it is desirable or convenient to have a variable with changing name or variable variable name. Specifically, the new variable will takes the value of the variable as the name of the variable. This is done by putting a dollar sign before the variable i.e. a variable name with a double dollar sign. Let's look at the following illustration:
Example
<?php /* A variable variable name */ $x = 'Jenny'; $$x = 'is our new student.'; #this means $$x = $Jenny; echo "$x ${$x} Let's welcome $x together."; ?>
In the above example, $x is the first variable and has a value "Jenny". The additional dollar sign before $x as $$x means that $$xx = $Jenny. Thus, the value of the first variable $x is used as a variable name in the second variable.
5. PHP Variable Declaration By Reference
There is another way to declare a PHP variable through a reference. Therefore, the value of a variable is obtained by using another variable. Also, this variable reference method uses an ampersand &, before the variable. This ampersand (&) assigns the value of one variable to another variable. Moreover, variables are not pointers like in the "C" language. Furthermore, three different operations can be performed using PHP variable references.
- Assign By Reference
- Return By Reference
- Pass By Reference
The subsequent example distinctly shows, how to assign PHP variable values by reference?
PHP Variable References
<?php /* PHP Variable Declaration by Reference */ $PHPTutorials = 'This is a PHP variable Tutorial.'; $tuts = &$PHPTutorials; /* A symbol ampersand(&) is used to assign value */ echo $tuts; ?>
6. PHP Variable Handling Function
Lastly, PHP has some predefined functions to manipulate variables. These functions are called variable handling functions. There is no need of any installation to access these variable handling functions. The table below contains all the variable handling functions.