View previous topic :: View next topic |
Author |
Message |
TigerDE2
Joined: 04 Jan 2003 Posts: 53 Location: Germany, Erfurt
|
Posted: Fri Mar 07, 2003 3:40 pm Post subject: Having troubles with variables - help! :) |
|
|
Hi!
I have some serious problems with vars:
I have three files, /main.php, /c/con.php and /c/func.php
They look like the following:
/main.php
Code: |
<?php
include('./c/con.php');
include('./c/func.php');
dosth();
?>
|
/c/con.php
Code: |
<?php
$text = "Hello World";
?>
|
/c/func.php
Code: |
<?php
function dosth () {
echo $text;
}
?>
|
If I run /main.php now, nothing happens.
If I change it into dosth($text); in /main.php
and function dosth($msg) { echo $msg; } in /c/func.php,
it works.
Why?
Is there a way to get it working as it does without parameter?
Thanks.
Christian _________________ Make me happy.
Correct my awful grammar and spelling mistakes...
-----
b2 is now WordPress.
Names change, excellence remains. Go, change. |
|
Back to top |
|
 |
dtdgoomba
Joined: 05 Aug 2002 Posts: 179 Location: Cambridge, MA
|
Posted: Fri Mar 07, 2003 4:04 pm Post subject: |
|
|
i think if you put
global $text;
above your echo text in the function it may work...
a function is in a differenct "scope" than the things outside of it from what I was told from friends so that's why you have to say in your function that you are going to grab the variables defined somewhere outside.
that's why you see them in the b2functions. I know this is a pretty lame description to real programmers so someone can follow this up and explain it a little better than I can... yeah, i went to mit but wasn't a comp sci guy so.... |
|
Back to top |
|
 |
TigerDE2
Joined: 04 Jan 2003 Posts: 53 Location: Germany, Erfurt
|
Posted: Fri Mar 07, 2003 4:13 pm Post subject: Thanks. |
|
|
Well, it worked. Thank you.
But how would you define a global var inside a function then?
I mean, one, that can be accessed from outside the function, too... _________________ Make me happy.
Correct my awful grammar and spelling mistakes...
-----
b2 is now WordPress.
Names change, excellence remains. Go, change. |
|
Back to top |
|
 |
nilson
Joined: 06 Jun 2003 Posts: 9
|
Posted: Fri Jun 06, 2003 6:05 pm Post subject: |
|
|
Try:
Code: |
$_GLOBAL['varname'];
|
But is the variable is declared in the fuction, it is not available at all until the fuction is executed. _________________ Mozilla Firebird (June 3rd) / Mozilla Thunderbird (June 3rd):: Windows XP :: 512 RAM
http://nilsonscorner.dyndns.org/ or http://home.earthlink.net/~nilsoncain/ |
|
Back to top |
|
 |
|