p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: how to make variables global??


Message #1 by "Ben Rigby" <ben@a...> on Sat, 21 Jul 2001 02:08:58
hi. i'm trying to define variables in the body of my script and to have 

them persist in my functions without having to pass them into my function 

(because i have so many to pass in). i've tried using the "global" 

keyword, but it doesn't work. for example i'd like to do the following:



$my_var="hello";

my_function();



function my_function(){

  echo "my variable is: $my_var";

}



how would i do this? any help would be MUCH appreciated. thanks!

-ben
Message #2 by BunchOfPixels@a... on Sat, 21 Jul 2001 03:36:51 EDT

$my_var="hello";

my_function();



function my_function(){

    global $my_var;

  echo "my variable is: $my_var";

}





Message #3 by paul@d... on Sat, 21 Jul 2001 04:00:09 -0400
Did you try something like this?



$month="July";

showMonth();



function showMonth (){

    // Temporarily convert a global variable into a local variable.

    $month=$GLOBALS["month"];

    echo $month;

}





Ben Rigby wrote:



> hi. i'm trying to define variables in the body of my script and to have

> them persist in my functions without having to pass them into my function

> (because i have so many to pass in). i've tried using the "global"

> keyword, but it doesn't work. for example i'd like to do the following:

>

> $my_var="hello";

> my_function();

>

> function my_function(){

>   echo "my variable is: $my_var";

> }

>

> how would i do this? any help would be MUCH appreciated. thanks!

> -ben

>



Message #4 by LUIS M MORALES <luismorales@j...> on Sat, 21 Jul 2001 09:29:42 -0400
Well,



When you call the variable on any functions you must be use the golbal

statement.



eg...



<?



$t = 'help';



function caca ($x){

  global $t;  // <-- here you use the $t variable and her value



  return $x.$t;

}



?>





Best regards,



Luis Morales



paul@d... wrote:



> Did you try something like this?

>

> $month="July";

> showMonth();

>

> function showMonth (){

>     // Temporarily convert a global variable into a local variable.

>     $month=$GLOBALS["month"];

>     echo $month;

> }

>

> Ben Rigby wrote:

>

> > hi. i'm trying to define variables in the body of my script and to have

> > them persist in my functions without having to pass them into my function

> > (because i have so many to pass in). i've tried using the "global"

> > keyword, but it doesn't work. for example i'd like to do the following:

> >

> > $my_var="hello";

> > my_function();

> >

> > function my_function(){

> >   echo "my variable is: $my_var";

> > }

> >

> > how would i do this? any help would be MUCH appreciated. thanks!

> > -ben

>



Message #5 by "Ben Rigby" <ben@a...> on Tue, 24 Jul 2001 08:11:29
hi. thanks for all of the responses. yes, i have tried the using the "global" keyword within the
function. 

but it's not working - i should have been more specific about my code - i think i simplified too much. i'm 

trying to do something more like this: 



$my_var="$45";

$my_code=my_function();

echo $my_code;



function my_function(){

  $my_code_local='Hi. You\'re total bill is: '. $my_var .' - Please pay promptly';

  return ($my_code_local)

}





that's about it - but with lots more variables and hundreds of lines in the  $my_code_local variable 

assignment.



any ideas? if i try "global $my_var"  - i get an error...



thanks! 

-ben
Message #6 by Jesse Goerz <jgoerz@l...> on Tue, 24 Jul 2001 07:56:05 -0500
On Tuesday 24 July 2001 03:11, Ben Rigby wrote:

> hi. thanks for all of the responses. yes, i have tried the using the

> "global" keyword within the function. but it's not working - i should have

> been more specific about my code - i think i simplified too much. i'm

> trying to do something more like this:

>

> $my_var="$45";

> $my_code=my_function();

> echo $my_code;

>

> function my_function(){

>   $my_code_local='Hi. You\'re total bill is: '. $my_var .' - Please pay

> promptly'; return ($my_code_local)

> }

>

>

> that's about it - but with lots more variables and hundreds of lines in the

>  $my_code_local variable assignment.

>

> any ideas? if i try "global $my_var"  - i get an error...



What's the error message?!  That's important to solving the problem.







Message #7 by root <paul@d...> on Tue, 24 Jul 2001 13:58:55 -0400
Why not pass the variable to the function instead of making it global? 

Wouldn't that be a better programming practice?

Message #8 by "Hermawan Haryanto" <hermawan@h...> on Wed, 25 Jul 2001 21:38:59
Helo there,

I don't know if you forgot this or not,

but I see in your returning syntax is not using the ";" sign after the end 

of syntax, maybe I'm wrong, but after I try on my server, after I add one 

more line :

global $my_var;

then the result is returning like this :

Hi. You're total bill is: $45 - Please pay promptly



Thanks

Hermawan Haryanto



> hi. thanks for all of the responses. yes, i have tried the using 

the "global" keyword within the function. 

> but it's not working - i should have been more specific about my code - 

i think i simplified too much. i'm 

> trying to do something more like this: 

> 

> $my_var="$45";

> $my_code=my_function();

> echo $my_code;

> 

> function my_function(){

>   $my_code_local='Hi. You\'re total bill is: '. $my_var .' - Please pay 

promptly';

>   return ($my_code_local)

> }

> 

> 

> that's about it - but with lots more variables and hundreds of lines in 

the  $my_code_local variable 

> assignment.

> 

> any ideas? if i try "global $my_var"  - i get an error...

> 

> thanks! 

> -ben

  Return to Index