Page 1 of 1
Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 1:44 am
by JAB Creations
I'm feeling a little

after I'm able to use plain variables in file 1.php that are declared in 2.php. However I'm not able to use a variable from test-includes.php in test.php. Files 1.php and test.php are the parents that then later include their respective includes files.
So what am I missing here? How come I can do this...
1.php
Code: Select all
<?php
include("2.php");
echo $title;
?>
2.php
...but I can't seem to get a value pulled across a file from a variable when any procedural programming is involved.
So I assigned the variables outside of the programming and then assigned them to other variables so...
$var1 in 1.php is pulled from 2.php
On 2.php $var1 = $var2
$var2 is defined within procedural programming.
I can go in to further details tomorrow but I'm already being dragged in to unconsciousness.
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 3:23 am
by aaronhall
One of my favorite ways to drive myself crazy is to have more than one copy of a file in the directory tree and reference the wrong one.
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 2:10 pm
by JAB Creations
That was not helpful.
My question is pretty clear: what makes a variable not accessible to another file?
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 2:24 pm
by Zoxive
JAB Creations wrote:That was not helpful.
My question is pretty clear: what makes a variable not accessible to another file?
Your OP explanation is not clear though.
The only way the variable is not accessible is if it isn't the global scope, aka inside a function.
I think code example would be nice

Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 2:54 pm
by aaronhall
JAB Creations wrote:That was not helpful.
My question is pretty clear: what makes a variable not accessible to another file?
Okay... to answer more directly: Make sure you're including the right file. Make sure the variable your defining isn't trapped in a scope that isn't accessible to the scope its being included to. Set something to $GLOBALS and see if you can access it. By "procedural programming" I assume you mean "anything that doesn't have a class". Functions still have scope. If you post some more information, maybe I can be more helpful.
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 3:24 pm
by JAB Creations
It would have helped to just been given a link to this page...
http://us3.php.net/global
It was actually pretty clear in it's explanation.
Local (scope) variable = variable set within a function and therefor can not be accessed via other files.
Global (scope) variable = variable not set within a function and therefor can be accessed via other files.
Therefor to gain access to a local scope variable it should be passed on to a global scope variable after it has been defined. Without worrying about any specifics here is the general idea...
1.php
Code: Select all
<?php
if ($a = $b) {$c = $d;}
$e = $d
?>
2.php
Code: Select all
<?php
include("1.php");
echo $e;
?>
Previously last night I attempted to set
$e but
before $d was defined.
Yay I learned something today...that I should have known a while ago. At least I'm not at the top of Mount Chimborazo without boots. Further thoughts are welcomed!

Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 3:44 pm
by Zoxive
JAB Creations wrote:Code: Select all
<?php
if ($a = $b) {$c = $d;}
$e = $d
?>
What is the point of this?
To check is something is equal use = x2 (== or ===). You are actually setting $a as $b in the if statement.
I guess I never got what the problem was, but I hope you figured it out

Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 3:56 pm
by JAB Creations
Without worrying about any specifics
I wanted to get $d on the second page but because it was a local variable I had to pass it's value on to a global variable. That is why I said don't worry about any specifics, it was a general concept example, not actual working code.
The problem was I was unaware of the clarification or existence of global versus local variables which I clearly stated the difference in my last post. I also made it clear that I am now aware of how to access a local variable by passing it's value on to a global variable.
"and there was much rejoicing." "yay"
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 4:00 pm
by Zoxive
JAB Creations wrote:
The problem was I was unaware of the clarification or existence of global versus local variables which I clearly stated the difference in my last post. I also made it clear that I am now aware of how to access a local variable by passing it's value on to a global variable.
$d in that if statement is in the global scope though.
Now if that was a function or class, not returning that data that is a different story.
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 4:18 pm
by JAB Creations
Ok now I'm back to being confused...
So this means $a and $b are local while $c, $d, and and $e are global?
No wait I've got if/else statements mixed up with functions.
So that means $a, $b, $c, $d, and $e are all global?
Moving along then...
Ok so I decided to conjure up an actually working example of where I'm not able to pull a variable's value through an includes by wrapping the scenario within a function...
file1.php
Code: Select all
<?php
include("file2.php");
echo $browser;
?>
file2.php
Code: Select all
<?php
function myfunction() {
$useragent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/Mozilla/", $useragent)) {$thebrowser = "Claims to be Mozilla";}
else {$thebrowser = "Does not claims to be Mozilla";}
}
myfunction();
$browser = $thebrowser;
?>
So my question now becomes this: how can I get $thebrowser value assigned to $browser?
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 5:24 pm
by anto91
JAB Creations wrote:Ok now I'm back to being confused...
So this means $a and $b are local while $c, $d, and and $e are global?
No wait I've got if/else statements mixed up with functions.
So that means $a, $b, $c, $d, and $e are all global?
Moving along then...
Ok so I decided to conjure up an actually working example of where I'm not able to pull a variable's value through an includes by wrapping the scenario within a function...
file1.php
Code: Select all
<?php
include("file2.php");
echo $browser;
?>
file2.php
Code: Select all
<?php
function myfunction() {
$useragent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/Mozilla/", $useragent)) {$thebrowser = "Claims to be Mozilla";}
else {$thebrowser = "Does not claims to be Mozilla";}
}
myfunction();
$browser = $thebrowser;
?>
So my question now becomes this: how can I get $thebrowser value assigned to $browser?
Code: Select all
<?php
function myfunction() {
$useragent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/Mozilla/", $useragent)) {$thebrowser = "Claims to be Mozilla";}
else {$thebrowser = "Does not claims to be Mozilla";}
return $thebrowser;
}
$browser = myfunction();
?>
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 9:13 pm
by JAB Creations
It works and I can duplicate it though I don't clearly understand php.net's page on return. Is this the main intended behavior for return or is it for something else?
Re: Cross-File Variables - When they do and don't work?
Posted: Fri Mar 14, 2008 9:32 pm
by Christopher
JAB Creations wrote:It works and I can duplicate it though I don't clearly understand php.net's page on return. Is this the main intended behavior for return or is it for something else?
There is really only one way to use return. It exits a function and makes the value of the function be the value specified in the return statement (or null if none). Functions implicitly do a
return; when they reach the end paren. A return is to a function like exit is to a program.