Current location: Hot Scripts Forums » Programming Languages » PHP » global variable problem with functions


global variable problem with functions

Reply
  #1 (permalink)  
Old 06-24-07, 08:54 PM
Dan Man Dan Man is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
global variable problem with functions

Hi,

I am writing some code, and I am having a problem, and after some checking the issue is as follows:

I have a variable that is declared in a file, lets say file1.php.

Now in my lib.php, i include "file1.php" at the top, and lib.php consists of several functions.

When i use any of the functions in lib.php, they are, for some reason, not in the scope of the variable declared in file1.php.

I even tried declaring the variable as a global variable and it did not work.

The only resolution i could find was passing the variable as a parameter, and that works, but i can not do that in my situation, and need to get it working the other way

any help is appreciated!!
Reply With Quote
  #2 (permalink)  
Old 06-24-07, 09:27 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
You would need to post your code to get specific help with it.

100 different people could write 100 differently coded ways to accomplish the same task. Something specific with the code you wrote is preventing the variables from working and no one could possibly know what you have written unless you post your code.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #3 (permalink)  
Old 06-25-07, 12:28 AM
fyrestrtr fyrestrtr is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?php

#File 1

$var 'hello';
?>
PHP Code:

<?php

#File 2
include 'file1.php';

function 
show($foo)
{
    global 
$var;
    echo 
$foo.' '.$var;
}

show('world');
?>
__________________
Find me at WHT
Reply With Quote
  #4 (permalink)  
Old 06-25-07, 02:51 AM
csmaster2005 csmaster2005 is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
fyre - that's exactly what the issue is (I'm dan man's partner, and we're having that problem)

if anyone could provide some input, that'd be great.
thanks
Reply With Quote
  #5 (permalink)  
Old 06-25-07, 08:20 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Read my post above.

Mind reading does not work over the Internet. Something you are specifically doing or not doing in your code is causing the problem. There could be any number of reasons why this is not working and the only way to get help with the exact problem is if you post your code, otherwise this will just be a game of twenty questions and guesses for answers.

Here are my guesses -

One or more include() statements are failing because the file path is incorrect.

The order of the include() statements are such that the variable does not exist at the time the function call is executed.

The variable(s) are being overwritten by an assignment statement "=" instead of a comparison operator "==".

Typo's in the code.

A problem with register global variables overwriting variables by the same name.

"I even tried declaring the variable as a global variable and it did not work." - Because this was something that was tried after the fact and not already present in the code, it shows that variable scope was not being handled correctly and the global declaration was probably done incorrectly. But we don't know if this was done correctly or not because no code has been posted.

To provide someone with the big picture of what your code is actually doing and to get the quickest solution for your problem, post your code.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #6 (permalink)  
Old 06-25-07, 03:19 PM
csmaster2005 csmaster2005 is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
mab, there's no reason to post all of our code. Our code is long and many parts, and we'd rather not share it all.

what fyrestrstr posted is exactly the issue we're having. The only difference is, in file1 the variable should be an object, not just a string. But besides that, that's the issue we're having.
Reply With Quote
  #7 (permalink)  
Old 06-25-07, 10:18 PM
csmaster2005 csmaster2005 is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
anyways, got it working. I just realized fyrsrstr had actually posted a solution (I thought he was just showing what my problem was by writing the code test for the board)

Last edited by csmaster2005; 06-25-07 at 10:40 PM.
Reply With Quote
  #8 (permalink)  
Old 06-26-07, 01:43 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by csmaster2005 View Post
mab, there's no reason to post all of our code. Our code is long and many parts, and we'd rather not share it all.
I could not disagree with you more on this... I know you want to protect your code but at the same time what you said is the equivalent of me going up to a contractor who did not build my house and saying, "um yeah... in my house the pipes are making a souuunnnd or something..., got a solution? Then when he asks to have a look I say no, it's my house I don't want you in it.

At least next time blind fold map and walk him to the part of your house where the pipes are making the noise, let him have a look at the part of the code that is causing you a problem if you wanted a quick answer IMHO.

Glad you found your answer though =)
<3
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
  #9 (permalink)  
Old 06-26-07, 09:33 PM
csmaster2005 csmaster2005 is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
heh Nova, I understand what you mean, but I did explain exactly what the issue was, and the first post did figure it out

Anyways, thanks alot for the help, and in the future, I'll post actual code examples, that way you dont have to translate my english into code.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Global variable declaration lcwei81 PHP 1 02-24-06 04:22 PM
templating problem (how to forward file name as a variable) skyrat PHP 0 09-11-05 08:27 PM
Redirection back to a page from form submit DAL Perl 11 03-21-05 02:45 PM
PHP Error Fairnie PHP 8 06-26-04 07:15 AM
Function and variable problem perleo PHP 1 02-11-04 08:52 PM


All times are GMT -5. The time now is 06:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.