This is probably a classical question, but I didn't find any answers... so could somebody please explain this thing to me or give me a link. First, let's see how I'm used to variables from JavaScript...
// this is a global variable. it is not defined
// inside a function, if-else block, for loop or similiar.
// it is accessible anywhere.
var brother = 'Bob';
function initSister(){
// I can access brother.
brother = 'Kevin';
// this is a local variable. it is defined inside this
// initSister() function, and is hence only accessible here.
var sister = 'Lisa';
}
initSister();
// I can print brother, because it's global.
document.write(brother);
// but this leads to an error, because sister only existed
// inside initSister().
document.write(sister);
ok, and the same in Perl:
# this is a global variable, works like brother in the
# JavaScript example
$brother = "Bob";
sub initSister {
# I can access brother.
$brother = "Kevin";
# now I define a local variable using the "my" keyword.
my $sister = "Lisa";
}
&initSister;
# and... I can print the global brother
print "$brother\n";
# but not sister, that only existed inside initSister. error:
print $sister\n";
But... PHP!? there seems to be no keyword for defining a local variable inside a function. You simply define it like any variable, and it's automatically local. The real problem I've got is, that I can't access a global variable from inside a function?? that's insane... I must be doing something wrong. but what?
// our global brother again
$brother = "Bob";
function initSister(){
// I can't access my global brother!?
$brother = "Kevin";
// this is my local sister
$sister = "Lisa";
}
initSister();
// my brother is still Bob?
print($brother);
// this gives an error like supposed
print($sister);
thanks in php,you have to use the global keyword or $GLOBALS array...such as:
$chicken = 'chicken';
function initchicken()
{
$chicken = 'potato';
return $chicken;
}
function previously_chickened()
{
global $chicken;
$str = 'previous chicken was:'.$chicken;
return $str;
}
function multinational_chicken()
{
$chicken = $GLOBALS["chicken"];
$str = 'Global Chicken??????:'.$chicken;
return $str;
}
function chicken_border()
{
return $chicken;
}
initchicken(); //returns potato
previously_chickened(); //returns previous chicken was: chicken
multinational_chicken(); //returns Global Chicken??????:chicken
echo $chicken; //returns chicken
chicken_border(); //does not work
make sense?ah, okay. Thanks willamoose, I get it now I guess I will just need to get used to this slighly different system PHP uses.Originally posted by agent002
ah, okay. Thanks willamoose, I get it now I guess I will just need to get used to this slighly different system PHP uses.
yeah...it can be annoying at times...I spent hours on one problem I was having...and ended up figuring out I had to use the globals array Originally posted by willamoose
in php,you have to use the global keyword or $GLOBALS array...such as:
ahh don't confuse the two.
super global $GLOBALS is way different than global in functions.
function previously_chickened()
{
global $chicken;
$str = 'previous chicken was:'.$chicken;
return $str;
}
that is only global to that function, if you set a $GLOBALS
$GLOBALS['chicken'] = 'chicken';
function previously_chickened()
{
$str = 'previous chicken was:'.$GLOBALS["chicken"];
return $str;
}
it will be global to all the functions. you don't need global in the function.
besides, you never set the $GLOBALS["chicken"] anywhere.
edit: if you are not in a function you do not have to use $GLOBALS["chicken"] as you can use $chicken. this only works in other scripts besides functions.$GLOBALS['chicken'] = 'chicken';
function previously_chickened()
{
$str = 'previous chicken was:'.$chicken;
return $str;
}
$chicken is empty!
you should use $GLOBALS['chicken'] inside your function as well.yes, I was thinking of outside it. sorry guys, I was wrong on that one. it should be globals inside as well. but they are still different.
thanks illogiqueOriginally posted by illogique
$chicken is empty!
you should use $GLOBALS['chicken'] inside your function as well.
opps!sorry...I was still half asleep ...I barely slept all week x.xOriginally posted by willamoose
opps!sorry...I was still half asleep ...I barely slept all week x.x
okay..I'm STILL half asleep..I didn't even post it x.x
// this is a global variable. it is not defined
// inside a function, if-else block, for loop or similiar.
// it is accessible anywhere.
var brother = 'Bob';
function initSister(){
// I can access brother.
brother = 'Kevin';
// this is a local variable. it is defined inside this
// initSister() function, and is hence only accessible here.
var sister = 'Lisa';
}
initSister();
// I can print brother, because it's global.
document.write(brother);
// but this leads to an error, because sister only existed
// inside initSister().
document.write(sister);
ok, and the same in Perl:
# this is a global variable, works like brother in the
# JavaScript example
$brother = "Bob";
sub initSister {
# I can access brother.
$brother = "Kevin";
# now I define a local variable using the "my" keyword.
my $sister = "Lisa";
}
&initSister;
# and... I can print the global brother
print "$brother\n";
# but not sister, that only existed inside initSister. error:
print $sister\n";
But... PHP!? there seems to be no keyword for defining a local variable inside a function. You simply define it like any variable, and it's automatically local. The real problem I've got is, that I can't access a global variable from inside a function?? that's insane... I must be doing something wrong. but what?
// our global brother again
$brother = "Bob";
function initSister(){
// I can't access my global brother!?
$brother = "Kevin";
// this is my local sister
$sister = "Lisa";
}
initSister();
// my brother is still Bob?
print($brother);
// this gives an error like supposed
print($sister);
thanks in php,you have to use the global keyword or $GLOBALS array...such as:
$chicken = 'chicken';
function initchicken()
{
$chicken = 'potato';
return $chicken;
}
function previously_chickened()
{
global $chicken;
$str = 'previous chicken was:'.$chicken;
return $str;
}
function multinational_chicken()
{
$chicken = $GLOBALS["chicken"];
$str = 'Global Chicken??????:'.$chicken;
return $str;
}
function chicken_border()
{
return $chicken;
}
initchicken(); //returns potato
previously_chickened(); //returns previous chicken was: chicken
multinational_chicken(); //returns Global Chicken??????:chicken
echo $chicken; //returns chicken
chicken_border(); //does not work
make sense?ah, okay. Thanks willamoose, I get it now I guess I will just need to get used to this slighly different system PHP uses.Originally posted by agent002
ah, okay. Thanks willamoose, I get it now I guess I will just need to get used to this slighly different system PHP uses.
yeah...it can be annoying at times...I spent hours on one problem I was having...and ended up figuring out I had to use the globals array Originally posted by willamoose
in php,you have to use the global keyword or $GLOBALS array...such as:
ahh don't confuse the two.
super global $GLOBALS is way different than global in functions.
function previously_chickened()
{
global $chicken;
$str = 'previous chicken was:'.$chicken;
return $str;
}
that is only global to that function, if you set a $GLOBALS
$GLOBALS['chicken'] = 'chicken';
function previously_chickened()
{
$str = 'previous chicken was:'.$GLOBALS["chicken"];
return $str;
}
it will be global to all the functions. you don't need global in the function.
besides, you never set the $GLOBALS["chicken"] anywhere.
edit: if you are not in a function you do not have to use $GLOBALS["chicken"] as you can use $chicken. this only works in other scripts besides functions.$GLOBALS['chicken'] = 'chicken';
function previously_chickened()
{
$str = 'previous chicken was:'.$chicken;
return $str;
}
$chicken is empty!
you should use $GLOBALS['chicken'] inside your function as well.yes, I was thinking of outside it. sorry guys, I was wrong on that one. it should be globals inside as well. but they are still different.
thanks illogiqueOriginally posted by illogique
$chicken is empty!
you should use $GLOBALS['chicken'] inside your function as well.
opps!sorry...I was still half asleep ...I barely slept all week x.xOriginally posted by willamoose
opps!sorry...I was still half asleep ...I barely slept all week x.x
okay..I'm STILL half asleep..I didn't even post it x.x