convert local variable to global variable in php
Suppose you have a variable which has same name as declared within the function and outside the function. But that variable has different scope. If the variable is declared within the function then it is called local variable and if it declared outside the function then it is global variable. If we assign/change the value in local variable it does not effect global variable. So we have to [...]
Suppose you have a variable which has same name as declared within the function and outside the function. But that variable has different scope. If the variable is declared within the function then it is called local variable and if it declared outside the function then it is global variable.
If we assign/change the value in local variable it does not effect global variable. So we have to make local variable behave as global variable. The easy way to do this is to write global in front of local variable. See the code below:- [code lang=”php”] <?phpclass Person
{
var $name;
function set_name($data){
global $name;
$name = $data;
}
function get_name(){
global $name;
return $name;
}
}
?>[/code] In the code above i have write global in front of $name to make it global. Now if we change the value of $name within the function then it effects the global variable also.
Share this article
Written by : Junaid Rehman
I am a blogger and freelance web developer by profession. I love to blog and learn new things about programming and IT World.
Follow us
A quick overview of the topics covered in this article.
Latest articles
February 23, 2025
February 23, 2025
February 23, 2025
February 23, 2025
February 23, 2025