0

I include a PHP file to the HEADER of my WordPress site, which searches through a CSV file and returns me the variable I need. I am trying to have it included in the header because it's a variable I will need throughout the site later on. If I test it out and try to echo this variable from the included script, it works fine. However, in the rest of the site, if I try to call that variable it doesn't return anything.

I know that the variable is being created because I try to echo it and it works. But when the I try to use that variable from a different script, it doesn't work. Is there some kind of code I need to pass the variable over to the rest of the site?

3 Answers 3

1

Variables default to function level only, you have to pass them or globalize them if you want to use them elsewhere. Depending on how your script is laid out, you might make it an object property, in which case it will be available anywhere your object is available and in all methods of that object - another option is to use global $var, but that's a bad idea and bad coding practice - another is to put it into a session using $_SESSION['myVar'] = $var and then call it the same way - yet another way is to pass it through arguments such as $database->getRows($var) and then on the other side "public function getRows ($var)", now you have $var in that function by passing it.

Sign up to request clarification or add additional context in comments.

3 Comments

you really think it is wise to put things you want to pass from one php file to another via session vars? actually my example works just fine without any globalization or using any object property. it pretty much depends on where you include your file. if it is inside a function the variable inside the include will stay there. if it is on the base level it will be accessible everywhere throughout all included files.
I said it was an option, I wasn't advocating passing your SALT through sessions or something :P
Not only was it an option - it was THE option. I tried countless solutions, nothing worked and this was so simple I can't believe I overlooked it. I was trying to pass a $var from a 7 level deep include inside an if function to the outer most parent. As the $var was both inside an include AND inside a function AND declared AFTER being called - setting as a session was BRILLIANT! And I do think it was wise in this instance as I was merely passing a number 1 through 10 as a variable to set a class on an element and javascript was not an option - so BRAVO I spend hours looking for this solution!
0

Make sure you global $variable the variable everytime you want to use it in a new function, or even within a new script. This will make sure that the variable is available everywhere that you need it.

5 Comments

generally global is a bad idea.
@Dagon: totally agree. @gms8994: It's always better solution to pass vars through function parameters. Why global for new script?
That solved my issue, thanks gms8994! To those above, is there a better method to achieve the same result? Thanks again.
Sorry, didn't see the answers above when I wrote that.
@Wh1T3h4Ck5 Because he's just doing an include; if he's got functions, he likely already knows about parameters, etc. I agree that it's a "bad" idea, but it's a wordpress site already, where globals abound..
0

3 files:

a.php:

<?php
include("c.php");

var_dump("c is ".$c . " after include()");

function incit(){
    include("b.php");
    var_dump("b is ".$b . " inside incit()");
}

incit();
var_dump("b is ".$b . " after incit()");



?>

b.php:

<?php
$b="bear";
?>

c.php:

<?php
$c="car";
?>

output looks like this:

string(24) "c is car after include()" 
string(24) "b is bear inside incit()" 
string(19) "b is after incit()" 

so $b is only defined INSIDE the scope of the function while $c on the other hand is "globally" definde. So you have to watch in what scope you are using the include.

1 Comment

Sorry I don't understand this answer. When I added global $actual_zone; into the INCLUDED file, I was able to use that variable in the rest of my site. I hear that using globals is a bad idea (not sure why) but how can I achieve the same result using better coding practice?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.