[SOLVED] How to access variable from within function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

[SOLVED] How to access variable from within function?

Post 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]
Last edited by tomfra on Sun Jul 04, 2004 12:24 pm, edited 1 time in total.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Try ...

Code: Select all

function yourfunction() {

  global $variable_you_need;

  while....
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post 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"]
}

?>
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post 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
Post Reply