Variable in PHP

Rules of declaring variables in PHP
•A variable starts with the $ sign followed by the name of variable.
•A variable cannot start with a number.
•A variable can only contain alphabets, numbers and underscore.
•Variable names are case sensitive.

In PHP variable can be declared anywhere in the script
There are three different variable scopes:
•Global
•Local
•Static

Global

The variable which is declared outside function. If it is not initialized, zero is stored as default. It is created before the program’s global execution starts and lost when the program is terminated. We can share data as multiple functions can access the same global variable. When the value of the global variable is modified in one function changes are visible in the rest of the program. Parameters passing is not necessary for a global variable as it is visible throughout the program. Global variable can be accessed by any statement in the program. If too many variables is declared as global then they remain in the memory till program execution is completed which causes memory issue. Modification can be done by any functions, any statement written in the program can change the value of the global variable.


<?php

$d=7;                                     //global variable

function display ()

{

//echo “Access value outside function =$d”; (cannot be used here)

}

display();

echo “Access value outside function=$d”;

?>





Global Key word
If we want to access outside a function from code inside a function we have to use global keyword with in the function.

<?php

$d=7;                                     //global variable

Function display ()

{

global $d;

echo “Access value outside function =$d” <br/>

}

display();

echo “Access value outside function=$d”;

?>

 

 

Local

The variable is declared inside function. It is not initialized a garbage value is stored. It is created when the function starts execution and lost when the function terminate. We cannot share data as it can be accessed by only one function, common data is required to pass repeatedly . When the value of the local variable is modified in one function the changes are not visible in another function. Parameters passing is required to access the value in other function. It can be accessed with the help of statements inside a function in which they are declared. These variables are deleted as soon as any function is over and release the memory space which it occupies. The debugging process is quite tricky.


<?php

function display()

{

$d=7;  // Local Variable

echo “Access value inside function =$d”;

}

display ();

echo “Access value outside function =$d”;  // this is undefined

?>

 

 

<?php

$d=153;

function display()

{

$d=7;  // Local Variable

echo “Access value inside function =$d”;

}

display ();

echo “Access value outside function =$d”;

?>

 

 


 

Static Variable

A variable with in a function reset every time when we call it. In case if we need variable values to remain save even outside the function then we have to use static keyword.



<?php

function display()

{

Static $d=0;

$d++;

return $d;

}

echo display() . “<br/>”;

echo display() . “<br/>”;

echo display() . “<br/>”;

?>

 

 

Difference between echo and print
• Echo has no return value while print is more like PHP function so it returns value which is always set to 1
• Echo can take multiple parameters while print can take one argument.

echo “Hello World”;
echo “Hello”. “World”;

print “Hello”. “World”;
print 98.60;