HI please read this and tell me that why it outputs outsider bar?
<?php
$bar = “outside”;
Function ab()
{
$bar = “inside”;
}
Ab();
Echo $bar . “<br />”;
?>
Printable View
HI please read this and tell me that why it outputs outsider bar?
<?php
$bar = “outside”;
Function ab()
{
$bar = “inside”;
}
Ab();
Echo $bar . “<br />”;
?>
Because of scope. A variable declared outside of a function (and not as a class member) has global scope. A variable declared within a function has a scope of that function, even if it has the same name as a globally scoped variable - it's a different, independent variable.
This occurs so that function variables don't interfere with global ones, and so that your functions are reusable. Most commonly if you want to affect a global variable, you pass it (by value) as a function argument. In other situations you may want to pass by reference, or declare a global var within the function.
To further what Samba noted, in order to get the function ab to recognize $bar, you can pass it in, or within your function, note that you're pulling in an outside variable:
Code:<?php
$bar = “outside”;
Function ab($barParam)
{
$barParam = “inside”;
}
Ab($bar);
Echo $bar . “<br />”;
?>
I believe both of those will produce the same result... I know the second will will produce "inside" ... because I use that a lot... the first one I'm not 100% confident in ... and I don't have a means to test it right this second.Code:<?php
$bar = “outside”;
Function ab()
{
global $bar;
$bar = “inside”;
}
Ab();
Echo $bar . “<br />”;
?>
-tg
Nope, the first one won't output "inside"; this would:
...or this:Code:$bar = “outside”;
Function ab($barParam)
{
return “inside”;
}
$bar = Ab($bar);
Echo $bar . “<br />”;
Code:$bar = “outside”;
Function ab(&$barParam)
{
$barParam = “inside”;
}
Ab($bar);
Echo $bar . “<br />”;
Thanks for the clarification... makes sense... the & before the $ is akin to the byref in VB... that explains some of the problems I've had in the past, and why I always end up using the global method... that will clean up a lot for me - I've only done PHP for personal sites, so I've never followed up on the why's abnd whatfors before...
-tg
its little complicated to understand.
let me ask something, let me have a dry run of this program, please guide me with this little clicking point.
In first step a global variable will be created and assigning it a value 'Inside' right
then function will be defined
and then function will be called ok.
the problem to understand is that
When the function will be called so its body will be executed right, so after execution of body, where the control will go? will it go to again AB(); function or will it go to next Echo statement? Did you understand? :(:(
No... AB() will ONLY run WHEN IT IS CALLED... just because you define it, doesn't mean it runs right away. No different than any other procedure based language.
-tg
@ Samba and @ techgnome
I'm totally confused that whats going on
Would you both please make a dry run of my program step by step? I'm mind is not picking it at all.