Page 1 of 1

[SOLVED] How to access variable from within function?

Posted: Sun Jul 04, 2004 7:59 am
by tomfra
I have a while loop in a function and need to know the value of one variable which is defined in the 'root' part of the php code - i.e. above the function itself.

It looks like I can't read its value from that while loop so I have to define it again before the loop but in the same function. Any solution?

Thanks!

Tomas[/mysql_man]

Posted: Sun Jul 04, 2004 8:21 am
by Buddha443556
Try ...

Code: Select all

function yourfunction() {

  global $variable_you_need;

  while....

Posted: Sun Jul 04, 2004 8:27 am
by wwwapu
...or instead of global you can use autoglobal $GLOBALS table

Code: Select all

<?php
function yourfunction(){
while...
$use_here=$GLOBALS["variable_you_need"]
}

?>

Posted: Sun Jul 04, 2004 12:19 pm
by tomfra
Thanks! Very helpful. I thought it could be done with "global" but I was using it at a wrong place - before the function itself. I am still in "newbie mode". ;)

Tomas