Jump to content
xisto Community
Sign in to follow this  
OpaQue

Php : Variables Included Dont Work In Functions Variables from Included files dont work

Recommended Posts

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

<? // -----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


// now we call our function
doit();
?>
linenums:0'><? // -----VARIABLES --- //$c,$d....include 'config.php'; // INCLUDED THE CONFIG FILE// now we call our functiondoit();?>
Here, the $a variable in function doit() does not work. Instead it prints only

A value is

INSTEAD OF

A value is 10

Share this post


Link to post
Share on other sites

You need to add

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Another solution:

<?// -----VARIABLES --- //$a=10,$b....// -----FUCTIONS--- //function doit(){print "A value is " . $GLOBALS['a'];}?>

or as I read in a post above

<?// -----VARIABLES --- //$a=10,$b....// -----FUCTIONS--- //function doit(){global $a;print "A value is " . $a;}?>

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.