PHP Variables

Variables Variables are containers to store data. We can add or change or delete data from the variable and use them in our script. Here are some basic rules to create variables.

How to declare variables in PHP with rules and getting type of variables using gettype()

Declaring variables in PHP

  1. Start with $
  2. Next char after $ ( starting ) must be a letter or underscore ( numbers are not allowed )
  3. Only alphabet, number and underscore are allowed ( no space )
  4. variable names are case-sensitive
Variable should start with $
$var1='testing'; // correct
var1='testing';   // Wrong - error 
_var1='testing'; // wrong -error
5var='testing';  // wrong -error
Variable should contain numbers, alphabet and underscore ( A-z, 0-9 AND _ )
$var1='testing'; // correct 
$va!3='testing'; // wrong -error
$var^4='testing';// wrong -error 
After $ we can't use number, it must be alphabet or underscore
$_var1='testing'; // correct 
$1var='testing'; // wrong error
$var1='testing'; // correct
Variables are case sensitive

No need to declare variable type before using in PHP.

Type of Variables in PHP

  1. Boolean : True False ( two values only )
  2. Integer : whole numbers like 5 , 356
  3. Double : with decimal numbers like 5.6, 2. , 3.89
  4. String : "plus2net"
  5. Array : Keep multiple variables in an array
  6. Object : Used to access classes Example : Database connection object
  7. Resource : Holding reference to external resources
  8. NULL : Special variable , represents no value
We will use gettype() to check the variable type.
echo gettype($my_var); // boolean 

echo gettype($my_var); // integer

echo gettype($my_var); // bouble

echo gettype($my_var); // string  

echo gettype($my_var); // array

echo gettype($my_var); // NULL 
PHP variables can take any type of data and change their type within the page also. ( in above example $my_var changes with type of data ) . It depends on its most recent assignment.

Undefined variable

We must declare a variable before using. This code will generate warning.
echo $name1;
PHP Notice:  Undefined variable: name1 in D:\php_files\test.php on line 4
Before using the variable, we can check if it is declared or not by using is_set() or by using empty().
echo $name1;
echo " Variabe is not declared ";
Variabe is not declared 
We can unset a variable.

Scope of Variable

  1. Local
  2. Global
  3. Static

Examples of Local Global & Static variables in PHP & scope inside and outside a functions

Local & Global

The variables declared as global are available throughout the script ( including inside the functions ).
Variables declared inside functions is know as local variable ( to that function ) and are only available inside the function.
So local variables declared inside a page are not available inside the functions of the page.

function test(){
echo "Value inside the function:".$v1;

echo "<br>Value outside the function:".$v1;
Value inside the function:10
Value outside the function:4


The associative array $GLOBALS holds the name of the variables as key and content as value of the variable. Once the variable is declared as element of $GLOBAL variable, then it is available throughout the script (including inside the functions).
function test(){ 
echo $v3; // Output is 30
echo $v2; // Output is 20
More on Scope of variables in PHP functions

Static variables

Local variables get reset once the execution of function is over. We can retain the value and use them for subsequent function calls by declaring them as Static variable.
function my_function(){
static $my_static_variable=0;
$my_static_variable=$my_static_variable + 1 ;
$my_local_variable=$my_local_variable + 1 ;
echo "Static variable :".$my_static_variable;
echo ",Local Variable :".$my_local_variable;

echo "<br>";
Output is here. See how static variable value is retained after execution of first call of the function whereas the local variable is destroyed.
Static variable :1, Local Variable :1
Static variable :2, Local Variable :1

Life of a variable

All variables are used within the page or within the script present in a page. As you are reading this page some variables are used to display the content of the page. These variables are lost as soon as this page execution is completed at server and downloaded to your browser. So how to pass the data or retain the data across the pages ?
Unset variables Passing variables from one page to other

Predefined Variables

PHP provides some predefined variable which we can use directly in our script ( without declaring before ).

Super global

For some of them we need not use global $variable to access them inside function or methods.
Global Variables


Getting Type of Variable var_dump() isset() empty() unset() strval() Transfer variables between pages and between sites Cookies to store Infomation constant: getting the value stored in a constant urlencode: Encoding variablas before sending them through URL Session Variables:Starting storing and destroying Sessions with Session ID
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer