|
-
Feb 22nd, 2004, 02:15 PM
#1
Thread Starter
Stuck in the 80s
[Resolved] Include/Require Problem
This problem has really bothered me for a long time, but I've just tried to get around it. Now it's getting to be annoying, so I'm hoping there's a solution.
I'm having a problem including/requiring a file within a function, and still having variables it created be defined through a few more function calls. I know that probably doesn't make sense, so I trimmed down my code to the bare-minimum for an example.
index.php is the main page, which calls a display.php (for displaying news in my program). display.php calls config.php, which simply loads configuration variables into an array.
Code:
index.php
<?php
// There would normally be some type of if structure
// here, that's why a function is needed:
main();
function main() {
require 'display.php';
execute();
}
?>
display.php
<?php
require 'config.php';
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'];
}
?>
config.php
<?php
// this usually loads values form a database
$CONFIG['name'] = 'value';
?>
Now, the execute() function of display.php is supposed to output a variable value, but no dice.
My question to you is, what can I do to make this work, besides fudging around in the index.php file (since I want my users to be able to call it however they need to)?
Last edited by The Hobo; Feb 25th, 2004 at 11:58 AM.
-
Feb 22nd, 2004, 02:26 PM
#2
Thread Starter
Stuck in the 80s
Hmm, I changed the code in display.php to this:
Code:
display.php
<?php
global $CONFIG;
$CONFIG = require('config.php');
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'] . '<br />';
}
?>
And it started working. Can anybody explain why?
-
Feb 23rd, 2004, 05:08 AM
#3
Because you require display.php from inside the main function. Put the require outside main and the original code should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 23rd, 2004, 11:21 AM
#4
Thread Starter
Stuck in the 80s
But I don't want it outside of the main...
If I have:
Code:
function one() {
}
function two() {
}
function three() {
}
function four() {
}
function five() {
}
And display.php is only required for three(), I don't want it to have to be there every time, if uneeded.
Plus, this code is setup by the users of my program, so I don't where they'll place this code.
-
Feb 23rd, 2004, 11:22 AM
#5
Thread Starter
Stuck in the 80s
I figured since everything happened inside of main, that the variables would be available to every function called by main. I guess I was wrong.
It's working now, and I guess that's what matters.
-
Feb 23rd, 2004, 01:16 PM
#6
The simple problem is that if you include the file from within the function, the "global" variables in that file aren't global anymore. Inclusion is no more than literal inclusion of the other file:
Code:
//blabla
include("some/other.php");
// foo bar
simply becomes:
Code:
//blabla
?>contents of some/other.php<?php
// foo bar
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 23rd, 2004, 05:55 PM
#7
Thread Starter
Stuck in the 80s
I have no idea what you just said.
-
Feb 23rd, 2004, 05:59 PM
#8
Thread Starter
Stuck in the 80s
So if I do:
Code:
function func1() {
include 'file.php';
// here
}
Variables created in file.php wont be available at the //here point? I know they will be, since I've seen it happen. So if they are available there, and I do:
Code:
function func1() {
include 'file.php';
func2(); // call 2nd function within 1st function
}
function func2() {
global $variableCreated;
// here
}
If those variables exist, the func2 can't access them? Even though they exist in function?
Does that mean that global only looks for globals in the global scope (I don't know if that made sense).
IE:
Code:
<?php
$var1 = 0;
$var2 = 0;
$var3 = 0;
function func1() {
func2();
}
function func2() {
global $var4;
}
Does global look for $var4 where the other variables are declared only?
I just figured it would look up the scope by checking func1, then in the global section.
I know you said this is a simple problem, but I'm still confused...unless what I said above is right...then I understand it.
-
Feb 23rd, 2004, 07:45 PM
#9
Addicted Member
I'm just messing with PHP, but from my experience with the paradigms of programming, Perl, and ASP:
A PHP file is not a function unto itself, and a function doesn't see non-global variables outside of itself (it's scope).
When you did this:
Code:
<?php
require 'config.php';
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'];
}
?>
<?php
// this usually loads values form a database
$CONFIG['name'] = 'value';
?>
The execute() doesn't see $CONFIG and is declaring it, anew, as a global.
When you do this:
Code:
<?php
global $CONFIG;
$CONFIG = require('config.php');
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'] . '<br />';
}
?>
$CONFIG is being declared outside execute() as a global, and as such is seen by the function (provided it is called after the variable is declared).
I could be wrong, but I don't think PHP is that far from every other programming language I've worked with.
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
-
Feb 23rd, 2004, 08:05 PM
#10
Thread Starter
Stuck in the 80s
Interesting, but I'm not sure if that's right. I tried this:
Code:
test.php
<?php
require 'config.php';
example();
function example() {
global $CONFIG;
echo $CONFIG['name'];
}
?>
And it worked, but I think it illustrates what you said above about not working. If example() didn't see $CONFIG and created its on variable, then $CONFIG['name'] wouldn't exist.
Unless I missed what you were saying. Is that the same or something else?
I should also note that these two both work in my example:
Code:
<?php
global $CONFIG;
$CONFIG = require('config.php');
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'] . '<br />';
}
?>
<?php
global $CONFIG;
require 'config.php';
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'] . '<br />';
}
?>
-
Feb 24th, 2004, 06:55 AM
#11
To explain my last post, just take the three files in your first post. Becuase include() and require() just literally include the files, the PHP parser sees this:
Code:
<?php
// There would normally be some type of if structure
// here, that's why a function is needed:
main();
function main() {
?><?php
?><?php
// this usually loads values form a database
$CONFIG['name'] = 'value';
?><?php
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'];
}
?>
<?php
execute();
}
?>
Ok, clear this up a little by removing adjacent ?><?php pairs:
code]<?php
// There would normally be some type of if structure
// here, that's why a function is needed:
main();
function main() {
// this usually loads values form a database
$CONFIG['name'] = 'value';
function execute() {
global $CONFIG;
echo 'The var: ' . $CONFIG['name'];
}
execute();
}
?>[/code]
global indeed only uses variables that really are globals, it doesn't go up in scope.
So $CONFIG as it comes from config.php is a local variable of function main. In execute, you say that with $CONFIG you want to access the global variable CONFIG. Which is undefined.
This is why I said that it's better to only include files at the global level, it prevents such bugs.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 24th, 2004, 11:55 AM
#12
Thread Starter
Stuck in the 80s
Originally posted by CornedBee
This is why I said that it's better to only include files at the global level, it prevents such bugs.
I understand that. But as I said, I can't.
-
Feb 25th, 2004, 06:12 AM
#13
You just said that you don't want to.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 25th, 2004, 11:57 AM
#14
Thread Starter
Stuck in the 80s
This issue is resolved. Thanks for your time.
-
Feb 26th, 2004, 04:57 PM
#15
Frenzied Member
I know this is resoved but all you had to do was declare it as (super) global
$GLOBALS['config'] = $config['name'];
than every script and function can use it.
then this would have worked
Code:
<?php
$GLOBALS['config'] = $config['name'];
require 'config.php';
example();
function example() {
echo $config;
}
?>
-
Feb 26th, 2004, 11:11 PM
#16
Thread Starter
Stuck in the 80s
Does that clear everytime the script finishes executing?
-
Feb 26th, 2004, 11:22 PM
#17
Frenzied Member
it clears just like any other variable. or until it gets set again.
-
Feb 27th, 2004, 12:57 AM
#18
Thread Starter
Stuck in the 80s
Interesting. Thanks for that information.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|