Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Variables in functions

How to use a set of variables everywhere



9:23 pm on Nov 15, 2005 (gmt 0)

10+ Year Member

I was wondering how I can make use of variables in functions?

I have a document that starts by including a variables.php-file and then a functions.php file. Is there a way to get the variables in the variables.php-file to work inside the functions in the functions.php-file without sending them when I access the function?

Supposedly the use of global would do this, but any way I have tried this it doesn't work.

Also, does anyone know of a good newbie tutorial for PHP-functions?


9:39 pm on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Hi wavesurf,

Try this little tutorial at PHP Freaks:




3:25 am on Nov 16, 2005 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I've always thought the PHP Functions [php.net] manual pages were quite good too.


8:52 am on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

LOL, very true coop. In the past though I`ve found newbies find the PHP.net website a little daunting?



10:27 pm on Nov 20, 2005 (gmt 0)

10+ Year Member

I guess you've already found your answers in the references given before my post, but I feel this can help a bit those who review it seeking for the same question:

Variable scope in PHP
A variable declared outside any function is a global variable. It exists everywhere in the script. References to variables outside any function refer to global variables.
A variable declared inside a function is a local variable for that function. It only exists inside that function. References to variables inside any function refer to that function's local variables.
Some language variable names are superglobal. A reference to a variable that could refer to a superglobal always resolves to the superglobal.
There is a superglobal array called $GLOBALS[], which holds all global variables existing in the script, indexed by name.

$a = "this is the global variable \$a";
function example(){
$a = "this is example's local variable \$a";
echo $a . "\n" . $GLOBALS['a'];

This code would produce the following output:
this is example's local variable $a
this is the global variable $a

I guess this completely answers the original question and some more which are related.
Now, I'd like to give my opinion about the diference between phpfreaks and php.net resources:
The phpfreaks link given by dreamcatcher is a tutorial.
php.net strong point, the manual pages, are not a tutorial, but a reference.
A tutorial explains something following a 'didactic' order: if you need to understand variables to be able to understand functions, then a tutorial will explain variables first and functions later. It tries to explain something from the beginning (or from a stablished start point, in the case of most advanced tutorials) up to some point. It could be compared to a line going upwards.
In the other hand, a reference doesn't try to explain a whole, but all of its parts independently. It could be compared to a grid.
A tutorial is useful if you want to learn something, like a language. A reference is your tool when you already know the whole and have a doubt of a precise point, like the syntax for a given function.

Intelligence implies knowing which tool to use to resolve each problem.

Hoping be useful,


Featured Threads

Hot Threads This Week

Hot Threads This Month