Jump to content


Quick Question

  • Please log in to reply
5 replies to this topic

#1 Fira

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 24 September 2006 - 05:19 AM

I've been learning php for a month, and have made decent progress, but seem to have stumbled upon this:

I've an include file, named inc.php. Within it stores many variables, one of which is called $var.
I've another file, named function.php. It includes inc.php. Within function.php is a function named fun().

Would Fun() be able to recognize $var without any direct declarations within the function itself? And what if Fun() was called from another file that includes function.php?

Help is appreciated.

#2 alpine

  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 24 September 2006 - 05:51 AM

If you set $var as GLOBAL you can use it within the function fun()

#3 Fira

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 24 September 2006 - 05:55 AM

And global is only needed for usage of variables within functions?


Now I'm recieving the error

Parse error: parse error, unexpected '=', expecting ',' or ';' in c:\WEB_ROOT\includes\include.php on line 13

Here's line 13 of include.php:

global $epts = (mysql_result($result,0,'ep'));

#4 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 September 2006 - 06:13 AM


global $epts;
$epts = mysql_result($result,0,'ep');

also, unless you passed $result as an argument ot the function, you're going to have to declare it as a global too.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 24 September 2006 - 10:29 AM

Better than using globals - pass variables to the function as arguments and use return value.

So define the function as
fun ($result) {
    $res = mysql_result($result,0,'ep');
    return $res;

and call with

$epts = fun($result);
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 Fira

  • Members
  • PipPip
  • Member
  • 15 posts

Posted 25 September 2006 - 01:20 AM

Problem solved. Thanks everyone.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users