Prev: recursive
Next: MySQL Query Puzzle
From: David Mehler on 18 Jul 2010 18:37 Hello, I've got a file with a variable declared in it. For purposes of this post: $name = $_POST['name']; Now a little later in the same file I have a custom function call that outputs some information. In that information is an echo statement outputting $name: echo $name; I'm wondering do I have to have $name declared as a global variable within that function? For example: function customFunction() { global $name } I've tried it both ways and both ways it works, with and without the global statement. I was under the impression that to be useful in a function variables outside were not accessible. Thanks. Dave.
From: Paul M Foster on 18 Jul 2010 22:11 On Sun, Jul 18, 2010 at 06:37:30PM -0400, David Mehler wrote: > Hello, > I've got a file with a variable declared in it. For purposes of this post: > > $name = $_POST['name']; > > Now a little later in the same file I have a custom function call that > outputs some information. In that information is an echo statement > outputting $name: > > echo $name; > > I'm wondering do I have to have $name declared as a global variable > within that function? For example: > > function customFunction() { > global $name > } > > I've tried it both ways and both ways it works, with and without the > global statement. I was under the impression that to be useful in a > function variables outside were not accessible. > Thanks. > Dave. Variables declared outside a function are visible outside the function. Variables declared inside a function are visible only within that function. To use a "global" variable inside a function, you must declare it global, as: global $globalvar; Paul -- Paul M. Foster
From: Shreyas Agasthya on 19 Jul 2010 04:07 My two cents on this one. Modify the $name within the function and print it. Modify the$name outside the function (means the non-global-declared $name) and print it. You will know the difference. --Shreyas On Mon, Jul 19, 2010 at 7:41 AM, Paul M Foster <paulf(a)quillandmouse.com>wrote: > On Sun, Jul 18, 2010 at 06:37:30PM -0400, David Mehler wrote: > > > Hello, > > I've got a file with a variable declared in it. For purposes of this > post: > > > > $name = $_POST['name']; > > > > Now a little later in the same file I have a custom function call that > > outputs some information. In that information is an echo statement > > outputting $name: > > > > echo $name; > > > > I'm wondering do I have to have $name declared as a global variable > > within that function? For example: > > > > function customFunction() { > > global $name > > } > > > > I've tried it both ways and both ways it works, with and without the > > global statement. I was under the impression that to be useful in a > > function variables outside were not accessible. > > Thanks. > > Dave. > > Variables declared outside a function are visible outside the function. > Variables declared inside a function are visible only within that > function. To use a "global" variable inside a function, you must declare > it global, as: > > global $globalvar; > > Paul > > -- > Paul M. Foster > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Regards, Shreyas Agasthya
From: Simcha Younger on 19 Jul 2010 04:28 On Mon, 19 Jul 2010 13:37:12 +0530 Shreyas Agasthya <shreyasbr(a)gmail.com> wrote: > My two cents on this one. > > Modify the $name within the function and print it. > > Modify the$name outside the function (means the non-global-declared $name) > and print it. You will know the difference. > > --Shreyas > > On Mon, Jul 19, 2010 at 7:41 AM, Paul M Foster <paulf(a)quillandmouse.com>wrote: > > > On Sun, Jul 18, 2010 at 06:37:30PM -0400, David Mehler wrote: > > > > > Hello, > > > I've got a file with a variable declared in it. For purposes of this > > post: > > > > > > $name = $_POST['name']; > > > > > > I'm wondering do I have to have $name declared as a global variable > > > within that function? For example: > > > > > > function customFunction() { > > > global $name > > > } > > > > > > I've tried it both ways and both ways it works, with and without the > > > global statement. I was under the impression that to be useful in a > > > function variables outside were not accessible. > > > Thanks. > > > Dave. It sounds like 'register globals' is turned on in your php.ini, and therefore $name will be visible everywhere, since it is taken from $_POST['name']. -- Simcha Younger <simcha(a)syounger.com>
|
Pages: 1 Prev: recursive Next: MySQL Query Puzzle |