|
-
Nov 5th, 2007, 06:53 PM
#1
Thread Starter
Addicted Member
variables
i am pretty new to PHP and am having a little problem.
my code is like this.
Code:
function thing()
{
include("global_variables.php");
echo($variable1);
}
function thing2()
{
include("global_variables.php");
echo($variable2);
}
this is not how i want it to be. the question is, is it possible to include the global variables file just once to make it work with all the functions?
-
Nov 5th, 2007, 09:37 PM
#2
Re: variables
well there are no global variables. The best idea is to have a configuration file, or settings file where are the configs are made.
A config file might include MySQL connections, variables for site title, or other things. And on all the pages that you need the mysql connections, you would include the config file.
so for ex:
PHP Code:
//CONFIG FILE:
$mysitetitle = "My Cool Site";
And my page:
PHP Code:
<?php include("config.php"); ?>
<title><?php echo $mysitetitle; ?></title>
... html
My usual boring signature: Something
-
Nov 5th, 2007, 10:00 PM
#3
Re: variables
Dclamp, all variables initialised outside a function or class are global.
To use these within a function you must import them using the global keyword. Otherwise, you create a local variable of the same name, which is discarded once it goes out of scope.
PHP Code:
$bar = 10;
echo $bar;
function foo()
{
global $bar;
echo ++$bar;
}
echo $bar;
-
Nov 5th, 2007, 10:59 PM
#4
Re: variables
So i can use $bar on another page without including it?
My usual boring signature: Something
-
Nov 5th, 2007, 11:16 PM
#5
Re: variables
No. What would give you that idea?
-
Nov 6th, 2007, 01:02 AM
#6
Re: variables
well then it is really not required to set the variables as global then... right?
My usual boring signature: Something
-
Nov 6th, 2007, 01:07 AM
#7
Re: variables
I don't understand your question. Look up variable scope in the online manual if you want a more thorough explanation and let's leave this thread for the thread starter.
-
Nov 7th, 2007, 03:12 PM
#8
Re: variables
 Originally Posted by penagate
I don't understand your question. Look up variable scope in the online manual if you want a more thorough explanation and let's leave this thread for the thread starter.
He has clearly read my last PHP post out of context
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|