PHP Variable
A variable is a name given to a memory location that stores data at runtime.
The scope of a variable determines its visibility.
A Php global variable is accessible to all the scripts in an application.
A local variable is only accessible to the script that it was defined in.
Think of a variable as a glass containing water. You can add water into the glass, drink all of it, refill it again etc.
The same applies for variables. Variables are used to store data and provide stored data when needed. Just like in other programming languages, PHP supports variables too. Let’s now look at the rules followed when creating variables in PHP.
- All variable names must start with the dollar sign e.g.

- Variable names are case sensitive; this means $my_var is different from $MY_VAR

- All variables names must start with a letter follow other characters e.g. $my_var1. $1my_var is not a legal variable name.

- Variable names must not contain any spaces, “$first name” is not a legal variable name. You can instead use an underscore in place of the space e.g. $first_name. You cant use characters such as the dollar or minus sign to separate variable names.

Let’s now look at how PHP determines the data type depending on the attributes of the supplied data.
<?php $my_var = 1; echo $my_var; ?>
Output:
1
Floating point numbers
<?php $my_var = 3.14; echo $my_var; ?>
Output:
3.14
Character strings
<?php $my_var ="Hypertext Pre Processor"; echo $my_var; ?>
Output:
Hypertext Pre Processor
Use of Variables
Variables help separate data from the program algorithms.
The same algorithm can be used for different input data values.
For example, suppose that you are developing a calculator program that adds up two numbers, you can create two variables that accept the numbers then you use the variables names in the expression that does the addition.
Variable Type Casting
Performing arithmetic computations using variables in a language such as C# requires the variables to be of the same data type.
Type casting is converting a variable or value into a desired data type.
This is very useful when performing arithmetic computations that require variables to be of the same data type.
Type casting in PHP is done by the interpreter.
In other languages such as C#, you have to cast the variables. The code below shows type casting in C#.

The diagram below shows PHP implementing the above example.

PHP also allows you to cast the data type. This is known as explicit casting. The code below demonstrates explicit type casting.
<?php $a = 1; $b = 1.5; $c = $a + $b; $c = $a + (int) $b; echo $c; ?>
Output:
2
Above Code Output 2 The var_dump function is used to determine the data type. The code below demonstrates how to use the var_dump function.
<?php $a = 1; var_dump($a); $b = 1.5; var_dump($b); $c = "I Love PHP"; var_dump($c); $d = true; var_dump($d); ?>
Output:
int(1) float(1.5) string(10) "I Love PHP" bool(true)
Variable Scope
PHP has three different variable scopes:
- local
- global
- static
Local Variable scope
A variable declared within a function is called as LOCAL SCOPE and can only be accessed within that function.
Example
<!DOCTYPE html>
<html>
<body>
<?php
function myTest() {
$a = 10; // local scope
echo "<p>Variable a inside function is: $a</p>";
}
myTest();
// using x outside the function will generate an error
echo "<p>Variable a outside function is: $a</p>";
? >
</body>
</html>
Output:
Variable a inside function is: 10
Error msg: Undefined variable: a
Variable a outside function is:
Global Variable scope
A variable declared outside a function is called as GLOBAL SCOPE variable and can only be accessed outside a function:
Example
<!DOCTYPE html>
<html>
<body>
<?php $a = 10; // global scope
function myTest() {
// using a inside this function will generate an error
echo "<p>Variable a inside function is: $a</p>";
}
myTest();
echo "<p>Variable a outside function is: $a</p>";
? >
</html>
Output:
Error msg: Undefined variable: a
Variable a inside function is:
Variable a outside function is: 10
Static Variable Scope
A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
PHP Superglobal Variable
In PHP there is 9 Superglobals variable available these are:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
$_REQUEST
In PHP $_REQUEST is used to collect form data.
$_SERVER
$_SERVER is an super globalvariable containing information such as headers, paths, and script locations.
$_GET
In PHP $_GET is used to collect form data.
In GET you send information to server in two way :
- Through URL
- Through from
$_POST
In PHP $_POST is used to collect form data.
In POST you send information to server in one way that is