Cross-File Variables - When they do and don't work?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Cross-File Variables - When they do and don't work?

Post by JAB Creations »

I'm feeling a little :crazy: 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

Code: Select all

<?php
$title = 'hello world';
?>
...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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Re: Cross-File Variables - When they do and don't work?

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cross-File Variables - When they do and don't work?

Post by JAB Creations »

That was not helpful. :|

My question is pretty clear: what makes a variable not accessible to another file?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Cross-File Variables - When they do and don't work?

Post 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 :lol:
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Re: Cross-File Variables - When they do and don't work?

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cross-File Variables - When they do and don't work?

Post 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. :mrgreen:

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! :)
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Cross-File Variables - When they do and don't work?

Post 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 :mrgreen:
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cross-File Variables - When they do and don't work?

Post 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"
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Cross-File Variables - When they do and don't work?

Post 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.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cross-File Variables - When they do and don't work?

Post by JAB Creations »

Ok now I'm back to being confused... :roll:

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. :banghead:

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?
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Cross-File Variables - When they do and don't work?

Post by anto91 »

JAB Creations wrote:Ok now I'm back to being confused... :roll:

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. :banghead:

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();
?>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Cross-File Variables - When they do and don't work?

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Cross-File Variables - When they do and don't work?

Post 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.
(#10850)
Post Reply