Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Php : Variables Included Dont Work In Functions, Variables from Included files dont work
OpaQue
post Jul 8 2005, 06:17 AM
Post #1


Administrator
Group Icon

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
Go to the top of the page
 
+Quote Post
Hercco
post Jul 8 2005, 01:17 PM
Post #2


Super Member
Group Icon

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.
Go to the top of the page
 
+Quote Post
mastercomputers
post Jul 9 2005, 12:31 PM
Post #3


BUG.SWAT.PATROL
Group Icon

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
Go to the top of the page
 
+Quote Post
jvizueta
post Sep 15 2005, 06:33 AM
Post #4


Newbie [ Level 2 ]
Group Icon

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;
}
?>

Go to the top of the page
 
+Quote Post
Umar Shah
post Mar 25 2008, 10:08 PM
Post #5


Newbie [ Level 2 ]
Group Icon

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.

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How Do I Create And Write To Files?(4)
  2. PHP And Texts: Need Help With PHP String Functions(5)
  3. Deleting Files With PHP(6)
  4. User Authentication Session Handling Problems(14)
  5. How To Delete Files When Session Ends(4)
  6. How To Edit Php Files?(16)
  7. Quickly Create Form Variables(5)
  8. Help To Transfer Files Within Hosting Space Using Php(4)
  9. Php Long Variables(5)
  10. Php File Upload(3)
  11. Reading Files And Folders(1)
  12. How To Protect Included Files(10)
  13. Random With Functions?(4)
  14. Magic Quotes And $_files(3)


 



- Lo-Fi Version Time is now: 24th July 2008 - 08:09 PM