Re: functions and global variables

From: Date: Mon, 19 Jul 2010 02:11:01 +0000
Subject: Re: functions and global variables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-306977@lists.php.net to get a copy of this message
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

« previous php.general (#306977) next »