boardom Forum Index boardom
b2 message board
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Having troubles with variables - help! :)

 
Post new topic   Reply to topic    boardom Forum Index -> PHP help
View previous topic :: View next topic  
Author Message
TigerDE2



Joined: 04 Jan 2003
Posts: 53
Location: Germany, Erfurt

PostPosted: Fri Mar 07, 2003 3:40 pm    Post subject: Having troubles with variables - help! :) Reply with quote

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. Smile
Correct my awful grammar and spelling mistakes...
-----
b2 is now WordPress.
Names change, excellence remains. Go, change.
Back to top
View user's profile Send private message
dtdgoomba



Joined: 05 Aug 2002
Posts: 179
Location: Cambridge, MA

PostPosted: Fri Mar 07, 2003 4:04 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
TigerDE2



Joined: 04 Jan 2003
Posts: 53
Location: Germany, Erfurt

PostPosted: Fri Mar 07, 2003 4:13 pm    Post subject: Thanks. Reply with quote

Well, it worked. Thank you. Smile
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. Smile
Correct my awful grammar and spelling mistakes...
-----
b2 is now WordPress.
Names change, excellence remains. Go, change.
Back to top
View user's profile Send private message
nilson



Joined: 06 Jun 2003
Posts: 9

PostPosted: Fri Jun 06, 2003 6:05 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    boardom Forum Index -> PHP help All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2 © 2001, 2002 phpBB Group