|
|
|
|
![]() ![]() |
Jul 8 2005, 06:17 AM
Post
#1
|
|
|
Administrator Group: Admin Posts: 458 Joined: 26-August 04 Member No.: 1 |
Today, I came up with this strange PHP behaviour. Just wanted to know if anyone has any suggestions!
I make a common variable/function file called config.php. I put in my generally used functions in it. Suppose this is my file CODE <? // -----VARIABLES --- // $a=10,$b.... // -----FUCTIONS--- // function doit() { print "A value is " . $a; } ?> Here, suppose we execute this file directly. Since A has a global scope, it does work perfectly. But if this same file is imported in another file say, mainfile.php CODE <? // -----VARIABLES --- // $c,$d.... include 'config.php'; // INCLUDED THE CONFIG FILE // now we call our function doit(); ?> Here, the $a variable in function doit() does not work. Instead it prints only CODE A value is INSTEAD OF CODE A value is 10 |
|
|
|
Jul 8 2005, 01:17 PM
Post
#2
|
|
|
Super Member Group: Members Posts: 595 Joined: 4-September 04 Member No.: 228 |
You need to add
CODE global $a; Inside the function scope. If you have several values you need inside the off-file functions it's handy to make an array so that you don't have to "global XX" everything. |
|
|
|
Jul 9 2005, 12:31 PM
Post
#3
|
|
|
BUG.SWAT.PATROL Group: Members Posts: 624 Joined: 1-September 04 From: Auckland, New Zealand Member No.: 27 |
There's a noticable problem with your script OpaQue, and that is $a is global to the main script execution, but functions each have their own scope and $a is definitely not part of that functions scope, the $a used inside that function is definitely going to be undefined, so I still don't see how this script functions correctly with the correct output for the first test.
So all I could suggest is the scope of $a is correct and the programs output is correct, except I have no reason why it worked for the first test. If you did <?php $a = 10; function doit($a) { echo 'A is ' . $a; } doit($a); ?> Then it would work as intended as the global $a is sent to the function, however the function doit($a) is actually a different variable with a different scope, but the results are as intended. Cheers, MC |
|
|
|
Sep 15 2005, 06:33 AM
Post
#4
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 22 Joined: 13-September 05 Member No.: 8,434 |
Another solution:
CODE <? // -----VARIABLES --- // $a=10,$b.... // -----FUCTIONS--- // function doit() { print "A value is " . $GLOBALS['a']; } ?> or as I read in a post above CODE <? // -----VARIABLES --- // $a=10,$b.... // -----FUCTIONS--- // function doit() { global $a; print "A value is " . $a; } ?> |
|
|
|
Mar 25 2008, 10:08 PM
Post
#5
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 22 Joined: 25-March 08 Member No.: 29,382 |
In PHP you can use the global variables from other files by declaring them as global in the file you want to use.
this ensures that the variables are taken from the global scope. You may want to declare a similar variable in the file but some function func1 might still need to access the global copy. this can be achieved by declaring the variable as global in the function. Hope this was helpful. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 24th July 2008 - 08:09 PM |