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.
Printable View
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.
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.
OK
a sample script below
that outputs "Iam" without the "Nobody"PHP Code:$my_name = "nobody";
function test(){
$test_var = "Iam";
$test_var = $test_var.$my_name;
return $test_var;
}
echo(test());
Dont know if that would be the best way.PHP Code:<?php
$test="marvin";
function x($test){
$t="i am ";
$t=$t.$test;
return $t;
}
echo(x($test));
?>
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
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();
As what I've said there would be a better way. :D
Great! thanks...