global tricks??? Please help.
Moderator: General Moderators
global tricks??? Please help.
Hello, Could soneone help out this one here:
There are two files, f1.php and f2.php in TWO different directories.
These two files need to share a variable. Here is what I have:
In f1.php,
global $var1;
function ()
{
global $var1;
echo $var1;
}
in f2.php (no functions),
$var1 = "test";
BUT, the echo in f1.php gave empty output.
I checked php books and online resources. I tried "static". I tried global $var1 everywhere. ..... None of them worked.
Thanks,
Bill
There are two files, f1.php and f2.php in TWO different directories.
These two files need to share a variable. Here is what I have:
In f1.php,
global $var1;
function ()
{
global $var1;
echo $var1;
}
in f2.php (no functions),
$var1 = "test";
BUT, the echo in f1.php gave empty output.
I checked php books and online resources. I tried "static". I tried global $var1 everywhere. ..... None of them worked.
Thanks,
Bill
Re: global tricks??? Please help.
Please wrap any code you post with the [ code][/code ] tags.
Also, just reading your code, have you included the other file using:
in your f1.php?
Also, just reading your code, have you included the other file using:
Code: Select all
include("f2.php");Re: global tricks??? Please help.
Thank you.
I just tried
include("/home/content/catalog/login.php");
in f1.php
but, got errors
Fatal error: Cannot redeclare tep_db_connect() (previously declared in /home/content/catalog/includes/functions/database.php:13) in /home/content/catalog/includes/functions/database.php on line 13
I did not know how to try the tags. the php files already have {?php..... ?}
More help, please.
I just tried
include("/home/content/catalog/login.php");
in f1.php
but, got errors
Fatal error: Cannot redeclare tep_db_connect() (previously declared in /home/content/catalog/includes/functions/database.php:13) in /home/content/catalog/includes/functions/database.php on line 13
I did not know how to try the tags. the php files already have {?php..... ?}
More help, please.
Re: global tricks??? Please help.
Redeclaring things is usually a sign of having included a file already.
in which case, you should try using
this will only include a file if it has not been included already.
also, yes: you may have the <?php ?> tags, but when posting in the forum, it is far easier to read code using the
in which case, you should try using
Code: Select all
include_once("file.php");also, yes: you may have the <?php ?> tags, but when posting in the forum, it is far easier to read code using the
Code: Select all
or [php ]. it displays it nicely and is much more user friendly.
ideally, with your original example you should be able to access the variable if its like this:
[syntax=php]
#f1.php
include_once("f2.php"); // this will ensure that f2.php is only included once.
function showMyGlobalVar(){
global $myVar;
print $myVar;
}
# END f1.php
#f2.php
$myVar = "Hello World!";
# END f2.php[/syntax]Re: global tricks??? Please help.
Tried include_once, got the same errors.
I also tried to create a php hearder file that had nothing but global $var1. Then, both f1.php and f2.php included it. Still got errors.
I also tried to create a php hearder file that had nothing but global $var1. Then, both f1.php and f2.php included it. Still got errors.
Re: global tricks??? Please help.
Tried include_once, got the same errors.
I also tried to create a php hearder file that had nothing but global $var1. Then, both f1.php and f2.php included it. Still got errors.
I also tried to create a php hearder file that had nothing but global $var1. Then, both f1.php and f2.php included it. Still got errors.
Re: global tricks??? Please help.
Tried include_once, got the same errors.
I also tried to create a php hearder file that had nothing but global $var1. Then, both f1.php and f2.php included it. Still got errors.
I also tried to create a php hearder file that had nothing but global $var1. Then, both f1.php and f2.php included it. Still got errors.
Re: global tricks??? Please help.
Please watch your posts.. no need to tripple post
The reason your still receiving errors (im assuming the same error's as before) is that you are including the same files multiple times.
To prevent including the same files multiple times, you should replace your include() statements with include_once(), or if you are using require() then replace with require_once(), which should sort out the redeclaration problems.
But as for your original example, the code i posted does work. You just need to double check your include() statements.
Also this is just another note, im not sure how large your application may be or circumstances, but using globals in functions isnt usually the best approach. I know i try to avoid global variables as much as possible.
I tend to use the following for passing variables:
But of course if you have a large scale application in which the variable your trying to access is used a lot, then globals can be better to use, but can still generate hard to trace bugs in the application.
The reason your still receiving errors (im assuming the same error's as before) is that you are including the same files multiple times.
To prevent including the same files multiple times, you should replace your include() statements with include_once(), or if you are using require() then replace with require_once(), which should sort out the redeclaration problems.
But as for your original example, the code i posted does work. You just need to double check your include() statements.
Also this is just another note, im not sure how large your application may be or circumstances, but using globals in functions isnt usually the best approach. I know i try to avoid global variables as much as possible.
I tend to use the following for passing variables:
Code: Select all
#file1.php
include_once("myFile.php");
function printMyVar($myVar){
print $myVar;
}
printMyVar($helloWorldVar);Code: Select all
#myFile.php
$helloWorldVar = "Hello World!";