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.

Declaring variables in PHP

  1. Start with $
  2. Next char after $ ( starting ) must be a letter ( 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.

Scope of Variable

  1. Local
  2. Global
  3. Static
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 $v3; // Output is 7
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).

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 varaible :".$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 varaible :1, Local Variable :1
Static varaible :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 ?

passing the variables from one page to other

. Declaring variables Unset variables Passing variables from one page to other
Cookies to store Infomation


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

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