homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Variables in functions
How to use a set of variables everywhere

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

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)

Hi wavesurf,

Try this little tutorial at PHP Freaks:




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

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


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

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)

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,

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved