|
-
May 17th, 2006, 08:15 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Using a variable outside of a function
can anyone help me on how to use a variable oution of a function.
for example
i have a variable name $none outside of a function and i want to use that variable inside that function, it returns and empty string when i tried it.
-
May 17th, 2006, 08:36 PM
#2
Re: Using a variable outside of a function
Could you post the code on how you use the variable?
Basically what I did is just I call the function that has an argument for the variable in html script and pass the variable to it.
-
May 17th, 2006, 08:45 PM
#3
Thread Starter
Fanatic Member
Re: Using a variable outside of a function
OK
a sample script below
PHP Code:
$my_name = "nobody";
function test(){
$test_var = "Iam";
$test_var = $test_var.$my_name;
return $test_var;
}
echo(test());
that outputs "Iam" without the "Nobody"
-
May 17th, 2006, 08:58 PM
#4
Re: Using a variable outside of a function
PHP Code:
<?php
$test="marvin";
function x($test){
$t="i am ";
$t=$t.$test;
return $t;
}
echo(x($test));
?>
Dont know if that would be the best way.
-
May 17th, 2006, 09:05 PM
#5
Thread Starter
Fanatic Member
Re: Using a variable outside of a function
yes for now i'm restricted to using that, but i felt there should be a way of doin it without having to define the variable along with the function
-
May 17th, 2006, 09:06 PM
#6
Re: Using a variable outside of a function
Global variables need to be imported into functions using the global keyword. Otherwise, they are created anew inside the function's scope.
Superglobals, such as $_POST etc., do not require this.
PHP Code:
$my_name = "nobody";
function test()
{
global $my_name;
$test_var = 'I am' . $my_name;
return $test_var;
}
echo test();
-
May 17th, 2006, 09:08 PM
#7
Re: Using a variable outside of a function
As what I've said there would be a better way.
-
May 17th, 2006, 09:12 PM
#8
Thread Starter
Fanatic Member
Re: Using a variable outside of a function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|