My code messed up as the variables set within a function cannot be accessed outside it. How to make the variable set inside a function accessible outside the function??
1. return the variable (you can also return an array of vars):
<BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>function test(){
$varname=1;
return varname;
}[/code]
2. Make the var global
<BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
$varname=1;
function test(){
global $varname;
$varname=2;
}[/code]
Misja
[This message has been edited by Misja (edited July 28, 2000).]
Declare the variables global inside the function. For example:
<BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>function test ()
{
global $test_value, $test_value2;
$test_value = "testing";
$test_value2 = " scope";
}
function test2 ()
{
// declare the variables global
global $test_value, $test_value2;
// prints "testing scope" if the variables have been set
echo $test_value . $test_value2;
}
There are other ways as well you can pass the variable into the function by reference:
<BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
function ( &$myvar1, &$myvar2 ) {
$myvar1 = 1;
$myvar2 = 2;
}
[/code]
------------------
Karl Austin KDA Web Services
"Everyone has a photographic memory. Some just don't have film."
Bookmarks