PHP 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.
<?Php
$my_var=true;
echo gettype($my_var); // boolean 

$my_var=5;
echo gettype($my_var); // integer

$my_var=5.6;
echo gettype($my_var); // bouble

$my_var='plus2net.com';
echo gettype($my_var); // string  

$my_var=array('Mango','Banana','Apple');
echo gettype($my_var); // array

$my_var=Null;
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.

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.
<?Php
function my_function(){
static $my_static_variable=0;
$my_local_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;
}

my_function();
echo "<br>";
my_function();
?>
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

.
Visitors Rating
Your Rating




Google+

Post Comment This is for short comments only. Use the forum for more discussions.




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer