English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

In PHP, I need to declare a variable and be able to use it inside a function. Is there any way I can do this? I tried "Global $myvar=0;" but i get the following error:

Parse error: syntax error, unexpected '=', expecting ',' or ';' in C:\Program Files\EasyPHP 2.0b1\www\test.php on line 2

and if I take out the "=" ("Global $myvar=0; $new=0; echo $new;") I get this:
Parse error: syntax error, unexpected T_ECHO in C:\Program Files\EasyPHP 2.0b1\www\test.php on line 4

2007-04-03 15:46:17 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

the global call needs to be inside the function, but the variable initialization can be outside... for example:

$myvar = 0;

function test() {
global $myvar;

echo $myvar;
}

EDIT: even though you are able to initialize the variable outside of the function, you MUST use the word global inside of the function like i've shown above... just wanted to clarify. hope this helps :-)

the problem with this:
>>>>>("Global $myvar=0; $new=0; echo $new;")<<<<<

is that all of this is inside the ""... take them out

2007-04-03 15:55:13 · answer #1 · answered by Jen 2 · 0 0

before the function construct a global variable and use the global variable's instance inside... or you can pass it using the return part of the function. so you construct $a; function amal() { global $a; $a=3; } echo $a;

2016-03-29 00:22:14 · answer #2 · answered by Anonymous · 0 0

or


$myvariable = "Hello";

function myFunction($variable)
{
$string = $variable." World.";
return $string;
}

echo myFunction($myvariable);
?>

2007-04-05 05:33:58 · answer #3 · answered by rob 3 · 0 0

fedest.com, questions and answers