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.
<?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.

Undefined variable

We must declare a variable before using. This code will generate warning.
<?Php
error_reporting(E_ALL);
echo $name1;
?>
Output
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().
<?Php
error_reporting(E_ALL);
if(isset($name1)){
echo $name1;
}else{
echo " Variabe is not declared ";
}
?>
Output
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.
<?Php 
$v1=4;

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

test();
echo "<br>Value outside the function:".$v1;
?>
Output
Value inside the function:10
Value outside the function:4

GLOBALS

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).
$v1=4; 
$v2=3;
function test(){ 
$GLOBALS[v1]=10;
$GLOBALS[v2]=20;
$GLOBALS[v3]=$GLOBALS[v1]+$GLOBALS[v2];
} 
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.
<?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 variable :".$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 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

Questions


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
PHP
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    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 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer